Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Interests

  • Loading branch information...
commit 5ef870b48c591836ba9ae3ec6dc06457d861f5d1 1 parent 0d6245c
@xaviershay authored
View
4 app/helpers/form_helper.rb
@@ -1,6 +1,10 @@
module FormHelper
def setup_user(user)
user.address ||= Address.new
+ (Interest.all - user.interests).each do |interest|
+ user.interest_users.build(:interest => interest)
+ end
+ user.interest_users.sort_by! {|x| x.interest.name }
user
end
end
View
3  app/models/interest.rb
@@ -0,0 +1,3 @@
+class Interest < ActiveRecord::Base
+ validates_presence_of :name
+end
View
4 app/models/interest_user.rb
@@ -0,0 +1,4 @@
+class InterestUser < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :interest
+end
View
3  app/models/user.rb
@@ -1,7 +1,10 @@
class User < ActiveRecord::Base
has_one :address
+ has_many :interest_users
+ has_many :interests, :through => :interest_users
accepts_nested_attributes_for :address
+ accepts_nested_attributes_for :interest_users, :allow_destroy => true
validates_presence_of :email
end
View
9 app/views/users/_form.html.erb
@@ -23,6 +23,15 @@
</div>
<% end %>
+ <%= f.fields_for :interest_users do |ff| %>
+ <div>
+ <%= ff.hidden_field :id %>
+ <%= ff.hidden_field :interest_id %>
+ <%= ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0', '1' %>
+ <%= ff.label :_destroy, ff.object.interest.name %>
+ </div>
+ <% end %>
+
<div class="actions">
<%= f.submit %>
</div>
View
13 db/migrate/20110306221257_create_models.rb
@@ -11,6 +11,19 @@ def self.up
t.string :city, :null => false
end
+
+ create_table :interests do |t|
+ t.string :name, :null => false
+ end
+
+ ["Door Knocking", "Presenting", "Admin"].each do |x|
+ Interest.create!(:name => x)
+ end
+
+ create_table :interest_users do |t|
+ t.references :user
+ t.references :interest
+ end
end
def self.down
View
35 db/schema.rb
@@ -0,0 +1,35 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110306221257) do
+
+ create_table "addresses", :force => true do |t|
+ t.integer "user_id"
+ t.string "city", :null => false
+ end
+
+ create_table "interest_users", :force => true do |t|
+ t.integer "user_id"
+ t.integer "interest_id"
+ end
+
+ create_table "interests", :force => true do |t|
+ t.string "name", :null => false
+ end
+
+ create_table "users", :force => true do |t|
+ t.string "email", :null => false
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.