Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Foo < ActiveRecord::Base
  has_many :foo_bar_selections
  has_many :bars, :through => :foo_bar_selections
end

class FooBarSelection < ActiveRecord::Base
  belongs_to :foo
  belongs_to :bar
end

class Bar < ActiveRecord::Base
  has_many :foo_bar_selections
  has_many :foos, :through => :foo_bar_selections
end

bar_old = Bar.create :name => "old"
bar_new = Bar.create :name => "new"
foo = Foo.create :baz => "old", :bars => [bar_old]
foo.baz #=> "old"
foo.bars #=> [#<Bar id: 1, name: "old">] 
foo.baz = "new"
foo.bars = [bar_new]
foo.reload
foo.baz #=> "old"
foo.bars #=> [#<Bar id: 2, name: "new">] 

# variable 'baz' does not get saved, but 'bars' does
# QUESTION: is there any way to prevent 'bars' from getting saved?