Wes Gamble
2006-May-08 21:39 UTC
[Rails] Addressing attribute of object specified by has_many fails
I have two model objects, Document and Images. Here are the relevant declarations: class Document < ActiveRecord::Base has_many :images class Image < ActiveRecord::Base belongs_to :document The database tables exist and foreign keys are set up correctly. In my controller, on an initial request, calling @document.images (nothing in it yet) is fine. However, after I attempt to manipulate the images collection by doing: @document.images << images //in this case, images is an empty Array object and then trying to address @document.images, I get the following error: undefined method `images'' for #<Document:0x365d6b0> By looking at the session information, I can see that "images" is being treated as an instance variable of the document object, and not as an attribute - it is not listed in the attributes hash inside the document object in the session. Has anyone ever seen this behavior before? It''s like my document "forgets" that it "has" images and then I can no longer count on the ActiveRecord base methods to manipulate the images collection. Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Kian
2006-May-09 02:16 UTC
[Rails] Re: Addressing attribute of object specified by has_many fails
Wes Gamble <weyus@...> writes:> > I have two model objects, Document and Images. Here are the relevant > declarations: > > class Document < ActiveRecord::Base > has_many :images > > class Image < ActiveRecord::Base > belongs_to :document > > The database tables exist and foreign keys are set up correctly. > > In my controller, on an initial request, calling <at> document.images > (nothing in it yet) is fine. > > However, after I attempt to manipulate the images collection by doing: > > <at> document.images << images //in this case, images is an empty > Array object > > and then trying to address <at> document.images, I get the following error: > > undefined method `images'' for #<Document:0x365d6b0> >BTW, I could not repeat this error with Rails 1.1.2 in the console. However, I think you really want to do <at> document.images = images # where images == [] The << operator tries to append to argument ([]) to end of its receiver. It looks like AR is smart enough to avoid this (in my test) but maybe that''s what''s causing problems for you.> Thanks, > Wes >
Seemingly Similar Threads
- What is has_many :through really buying you over HABTM?
- checkboxes with a has_many :through relation
- HABTM join table has an "ID" column - is this an issue?
- text_field doesn''t call overridden ActiveRecord getters
- Wrap error_messages_for() call when no instance var present