John Johnson
2005-Feb-09 16:10 UTC
Associated collection''s associated collections aren''t being loaded?
When an Ecr is loaded by my ecr/show form, the associated collection''s collections aren''t being populated. My associations look like so: Part has_and_belongs_to_many :kits has_many :ecrs Kit has_and_belongs_to_many :programs has_and_belongs_to_many :cutfiles has_and_belongs_to_many :parts Cutfile has_and_belongs_to_many :kits I put a breakpoint in ecrs/show.rhtml, load ecrs/show/2 and this is what I see: irb(#<EcrsController:0x15e47f0>):005:0> @ecr.part.kits[0] => #<Kit:0x153cf8c @attributes={"kit_number"=>"2082M29KIT-D", "part_id"=>"1", "current_revision"=>"C", "kit_rawma_number"=>"G9076281E", "kit_id"=>"1", "quantity"=>"1", "id"=>"12"}> so the part has a kit (actually several) irb(#<EcrsController:0x15e47f0>):006:0> @ecr.part.kits[0].cutfiles => [] and the kit has no cutfiles irb(#<EcrsController:0x15e47f0>):007:0> @ecr.part.kits[0] = Kit.find(@ecr.part.kits[0].kit_id) => #<Kit:0x15174a8 @attributes={"kit_number"=>"2082M29KIT-D", "current_revision"=>"C", "kit_rawma_number"=>"G9076281E", "id"=>"1"}> I force the kit to be reloaded irb(#<EcrsController:0x15e47f0>):008:0> @ecr.part.kits[0].cutfiles => [#<Cutfile:0x150e808 @attributes={"cut_data"=>nil, "label_data"=>nil, "kit_id"=>"1", "quantity"=>"5", "id"=>"12", "revision"=>"A", "filename"=>"CD10000", "cutfile_id"=>"1"}>, #<Cutfile:0x150e7a4 @attributes={"cut_data"=>nil, "label_data"=>nil, "kit_id"=>"1", "quantity"=>"2", "id"=>"13", "revision"=>"A", "filename"=>"CD6060842", "cutfile_id"=>"2"}>] and now it has cutfiles, as it should. Any advice greatly appreciated! Regards, JJ
Jeremy Kemper
2005-Feb-09 16:24 UTC
Re: Associated collection''s associated collections aren''t being loaded?
John Johnson wrote:> When an Ecr is loaded by my ecr/show form, the associated collection''s > collections aren''t being populated.Associations are lazy-loaded when you first access them. jeremy
John Johnson
2005-Feb-09 18:45 UTC
Re: Associated collection''s associated collections aren''t being loaded?
On 09-Feb-2005, at 11:24, Jeremy Kemper wrote:> John Johnson wrote: >> When an Ecr is loaded by my ecr/show form, the associated collection''s >> collections aren''t being populated. > > Associations are lazy-loaded when you first access them. > jeremyIs there something I need to do to give it a hint? This is my code from the ecrs/show.rhtml page: <table> <% for kit in @ecr.part.kits %> <% kit = Kit.find(kit.kit_id) %> <tr><td>Kit</td><td><%= kit.kit_number %></td> <td> <table> <% for cut in kit.cutfiles %> <tr><td><%= cut.filename %></td></tr> <% end %> </table> </td> </tr> <% end %> </table> Without the kit = Kit.find(kit.kit_id) it doesn''t show any cutfiles. Regards, JJ
Jeremy Kemper
2005-Feb-09 19:11 UTC
Re: Associated collection''s associated collections aren''t being loaded?
John Johnson wrote:> On 09-Feb-2005, at 11:24, Jeremy Kemper wrote: >> John Johnson wrote: >>> When an Ecr is loaded by my ecr/show form, the associated collection''s >>> collections aren''t being populated. >> Associations are lazy-loaded when you first access them. > > Is there something I need to do to give it a hint? This is my code from > the ecrs/show.rhtml page: > > <table> > <% for kit in @ecr.part.kits %> > <% kit = Kit.find(kit.kit_id) %> > <tr><td>Kit</td><td><%= kit.kit_number %></td> > <td> > <table> > <% for cut in kit.cutfiles %> > <tr><td><%= cut.filename %></td></tr> > <% end %> > </table> > </td> > </tr> > <% end %> > </table> > > Without the kit = Kit.find(kit.kit_id) it doesn''t show any cutfiles.A wild guess without seeing your association declarations: do you have an id field in any of your habtm join tables? Get rid of it. jeremy