Guest User

Untitled

a guest
Jun 22nd, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. class Profile < ActiveRecord::Base
  2. has_one :address
  3. accepts_nested_attributes_for :address
  4. end
  5.  
  6. class Address < ActiveRecord::Base
  7. belongs_to :profile
  8. end
  9.  
  10. >> p = Profile.new
  11. => #<Profile id: nil, name: nil, created_at: nil, updated_at: nil>
  12. >> p.update_attributes(:name => "foo", :address_attributes => {:line1 => "my street"})
  13. => true
  14.  
  15. mysql> select * from profiles;
  16. +----+------+---------------------+---------------------+
  17. | id | name | created_at | updated_at |
  18. +----+------+---------------------+---------------------+
  19. | 2 | foo | 2009-06-29 12:53:58 | 2009-06-29 12:53:58 |
  20. +----+------+---------------------+---------------------+
  21. 1 row in set (0.00 sec)
  22.  
  23. mysql> select * from addresses;
  24. +----+------------+-----------+---------------------+---------------------+
  25. | id | profile_id | line1 | created_at | updated_at |
  26. +----+------------+-----------+---------------------+---------------------+
  27. | 3 | 2 | my street | 2009-06-29 12:53:58 | 2009-06-29 12:53:58 |
  28. +----+------------+-----------+---------------------+---------------------+
  29. 1 row in set (0.00 sec)
  30.  
  31. >> p = Profile.first
  32. => #<Profile id: 2, name: "foo", created_at: "2009-06-29 12:53:58", updated_at: "2009-06-29 12:53:58">
  33. >> p.update_attributes(:name => "josh", :address_attributes => {:line1 => "Other street"})
  34. => true
  35.  
  36. mysql> select * from profiles;
  37. +----+------+---------------------+---------------------+
  38. | id | name | created_at | updated_at |
  39. +----+------+---------------------+---------------------+
  40. | 2 | josh | 2009-06-29 12:53:58 | 2009-06-29 12:54:41 |
  41. +----+------+---------------------+---------------------+
  42. 1 row in set (0.00 sec)
  43.  
  44. mysql> select * from addresses;
  45. +----+------------+--------------+---------------------+---------------------+
  46. | id | profile_id | line1 | created_at | updated_at |
  47. +----+------------+--------------+---------------------+---------------------+
  48. | 3 | NULL | my street | 2009-06-29 12:53:58 | 2009-06-29 12:54:41 |
  49. | 4 | 2 | Other street | 2009-06-29 12:54:42 | 2009-06-29 12:54:42 |
  50. +----+------------+--------------+---------------------+---------------------+
  51. 2 rows in set (0.00 sec)
Add Comment
Please, Sign In to add comment