Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Profile < ActiveRecord::Base
- has_one :address
- accepts_nested_attributes_for :address
- end
- class Address < ActiveRecord::Base
- belongs_to :profile
- end
- >> p = Profile.new
- => #<Profile id: nil, name: nil, created_at: nil, updated_at: nil>
- >> p.update_attributes(:name => "foo", :address_attributes => {:line1 => "my street"})
- => true
- mysql> select * from profiles;
- +----+------+---------------------+---------------------+
- | id | name | created_at | updated_at |
- +----+------+---------------------+---------------------+
- | 2 | foo | 2009-06-29 12:53:58 | 2009-06-29 12:53:58 |
- +----+------+---------------------+---------------------+
- 1 row in set (0.00 sec)
- mysql> select * from addresses;
- +----+------------+-----------+---------------------+---------------------+
- | id | profile_id | line1 | created_at | updated_at |
- +----+------------+-----------+---------------------+---------------------+
- | 3 | 2 | my street | 2009-06-29 12:53:58 | 2009-06-29 12:53:58 |
- +----+------------+-----------+---------------------+---------------------+
- 1 row in set (0.00 sec)
- >> p = Profile.first
- => #<Profile id: 2, name: "foo", created_at: "2009-06-29 12:53:58", updated_at: "2009-06-29 12:53:58">
- >> p.update_attributes(:name => "josh", :address_attributes => {:line1 => "Other street"})
- => true
- mysql> select * from profiles;
- +----+------+---------------------+---------------------+
- | id | name | created_at | updated_at |
- +----+------+---------------------+---------------------+
- | 2 | josh | 2009-06-29 12:53:58 | 2009-06-29 12:54:41 |
- +----+------+---------------------+---------------------+
- 1 row in set (0.00 sec)
- mysql> select * from addresses;
- +----+------------+--------------+---------------------+---------------------+
- | id | profile_id | line1 | created_at | updated_at |
- +----+------------+--------------+---------------------+---------------------+
- | 3 | NULL | my street | 2009-06-29 12:53:58 | 2009-06-29 12:54:41 |
- | 4 | 2 | Other street | 2009-06-29 12:54:42 | 2009-06-29 12:54:42 |
- +----+------------+--------------+---------------------+---------------------+
- 2 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment