forked from xaviershay/nested-attributes-demo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d6245c
commit 5ef870b
Showing
7 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Interest < ActiveRecord::Base | ||
validates_presence_of :name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class InterestUser < ActiveRecord::Base | ||
belongs_to :user | ||
belongs_to :interest | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |