Skip to content

Commit

Permalink
Interests
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Mar 6, 2011
1 parent 0d6245c commit 5ef870b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 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
3 changes: 3 additions & 0 deletions app/models/interest.rb
@@ -0,0 +1,3 @@
class Interest < ActiveRecord::Base
validates_presence_of :name
end
4 changes: 4 additions & 0 deletions app/models/interest_user.rb
@@ -0,0 +1,4 @@
class InterestUser < ActiveRecord::Base
belongs_to :user
belongs_to :interest
end
3 changes: 3 additions & 0 deletions 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
9 changes: 9 additions & 0 deletions app/views/users/_form.html.erb
Expand Up @@ -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>
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20110306221257_create_models.rb
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions 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

0 comments on commit 5ef870b

Please sign in to comment.