Guest User

Untitled

a guest
Nov 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. class Foo < ActiveRecord::Base
  2. has_many :foo_bar_selections
  3. has_many :bars, :through => :foo_bar_selections
  4. end
  5.  
  6. class FooBarSelection < ActiveRecord::Base
  7. belongs_to :foo
  8. belongs_to :bar
  9. end
  10.  
  11. class Bar < ActiveRecord::Base
  12. has_many :foo_bar_selections
  13. has_many :foos, :through => :foo_bar_selections
  14. end
  15.  
  16. bar_old = Bar.create :name => "old"
  17. bar_new = Bar.create :name => "new"
  18. foo = Foo.create :baz => "old", :bars => [bar_old]
  19. foo.baz #=> "old"
  20. foo.bars #=> [#<Bar id: 1, name: "old">]
  21. foo.baz = "new"
  22. foo.bars = [bar_new]
  23. foo.reload
  24. foo.baz #=> "old"
  25. foo.bars #=> [#<Bar id: 2, name: "new">]
  26.  
  27. # variable 'baz' does not get saved, but 'bars' does
  28. # QUESTION: is there any way to prevent 'bars' from getting saved?
Add Comment
Please, Sign In to add comment