Can anyone explain this oddity to me? Given two classes as follows: class Question < ActiveRecord::Base belongs_to :category end class Category < ActiveRecord::Base has_many :questions end I get the following: >> q = Question.find 6789 => #<Question:0x37e9e70 @attributes={"id"=>"6789", "category_id"=>"8200"}> >> cat = q.category => #<Category:0x37e1cc8 @attributes={"id"=>"8200"}> >> q.category = nil => nil >> cat => nil The last line is the bit that''s confusing me. I can "fix" it as follows: >> q = Question.find 6789 => #<Question:0x37e9e70 @attributes={"id"=>"6789", "category_id"=>"8200"}> >> cat = q.category.dup => #<Category:0x37e1788 @attributes={"id"=>"8200"}> >> q.category = nil => nil >> cat => #<Category:0x37e1788 @attributes={"id"=>"8200"}> But why do I need the "dup"? This seems to break the principle of least surprise? Is there a better way to handle this? Thanks in advance for your help! paul.butcher->msgCount++ Snetterton, Castle Combe, Cadwell Park... Who says I have a one track mind? -- Posted via http://www.ruby-forum.com/.
This looks very strange, doesn''t it? What happens is that when you call q.category you get AssociationProxy object which pretends to be another object. After you call q.category = nil this proxy pretends to be a nil object. You can see it by inspecting the object id like puts cat.__id__ It should return something other than value 4 which is an id of the nil object. Kent. On 2/4/06, Paul Butcher <paul@paulbutcher.com> wrote:> Can anyone explain this oddity to me? > > Given two classes as follows: > > class Question < ActiveRecord::Base > belongs_to :category > end > > class Category < ActiveRecord::Base > has_many :questions > end > > I get the following: > > >> q = Question.find 6789 > => #<Question:0x37e9e70 @attributes={"id"=>"6789", > "category_id"=>"8200"}> > >> cat = q.category > => #<Category:0x37e1cc8 @attributes={"id"=>"8200"}> > >> q.category = nil > => nil > >> cat > => nil > > The last line is the bit that''s confusing me. > > I can "fix" it as follows: > > >> q = Question.find 6789 > => #<Question:0x37e9e70 @attributes={"id"=>"6789", > "category_id"=>"8200"}> > >> cat = q.category.dup > => #<Category:0x37e1788 @attributes={"id"=>"8200"}> > >> q.category = nil > => nil > >> cat > => #<Category:0x37e1788 @attributes={"id"=>"8200"}> > > But why do I need the "dup"? This seems to break the principle of least > surprise? Is there a better way to handle this? > > Thanks in advance for your help! > > paul.butcher->msgCount++ > > Snetterton, Castle Combe, Cadwell Park... > Who says I have a one track mind? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Kent Sibilev wrote:> This looks very strange, doesn''t it? > > What happens is that when you call > > q.category > > you get AssociationProxy object which pretends to be another object. > After you callThanks, Kent. OK - I understand what''s happening. I still think that it violates the principle of least surprise! :-) Regarding the second half of my question - is there a better way to handle the situation than "dup"? I notice that AssociationProxy has a "target" method - which does seem to do the trick. Is this the best solution - or is there an alternative I should be aware of? Thanks again! (BTW - is it just me, or are others having problems with the forum bridge at the moment?) paul.butcher->msgCount++ Snetterton, Castle Combe, Cadwell Park... Who says I have a one track mind?