Hello everyone, I''ve got the following scenario Controller @things = Thing.find_all @thing_pages, @things = paginate :things, :per_page => 10, :order_by => ''otherthings.otherthingname, thingname'', :joins => ['',otherthings where things.id = otherthings.thing_id''] View for thing in @things <%= thing.id %> <%= thing.thingfield1 %> What I''d expect to see is a list of the id fields for records in the **things** table; instead I''m seeing a list of the id fields for records in the **otherthings** table. However, ''thing.thingfield1'' gives exactly what I want - a list of the thingfield1 fields for records in the things table. It seems that the join in the controller code has picked up the id field from the otherthings table, rather than the id field from the things table. I''ve confirmed that the model definitions for both ''things'' and ''otherthings'' is correct. Has anyone else encountered this, and/or know of a way around it? I''m using this block of code all over the place (with different table names) in my app, and it''s working perfectly everywhere except this one place. Thanks in advance Dave M.
You need to add :select => ''things.*'' to pagination options. This happens because there are more than one columns with name ''id'' (things.id and otherthings.id). Which column will prevail is quite a gamble without qualification... izidor On Dec 8, 2005, at 11:03 AM, David Mitchell wrote:> Hello everyone, > > I''ve got the following scenario > > Controller > @things = Thing.find_all > @thing_pages, @things = paginate :things, :per_page => 10, :order_by > => ''otherthings.otherthingname, thingname'', :joins => ['',otherthings > where things.id = otherthings.thing_id''] > > View > for thing in @things > <%= thing.id %> > <%= thing.thingfield1 %> > > What I''d expect to see is a list of the id fields for records in the > **things** table; instead I''m seeing a list of the id fields for > records in the **otherthings** table. However, ''thing.thingfield1'' > gives exactly what I want - a list of the thingfield1 fields for > records in the things table. > > It seems that the join in the controller code has picked up the id > field from the otherthings table, rather than the id field from the > things table. I''ve confirmed that the model definitions for both > ''things'' and ''otherthings'' is correct. > > Has anyone else encountered this, and/or know of a way around it? I''m > using this block of code all over the place (with different table > names) in my app, and it''s working perfectly everywhere except this > one place. > > Thanks in advance > > Dave M. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Yes, I encountered it. The id for the second table has the same name as the first and thus the first is ovewritten by the second. No problem. You use :select=> and then when you select the id column of the second table you change its name like secondtable.id as ''foo_id'' bruce On 8-Dec-05, at 6:00 AM, Izidor Jerebic wrote:> > You need to add :select => ''things.*'' to pagination options. This > happens because there are more than one columns with name > ''id'' (things.id and otherthings.id). Which column will prevail is > quite a gamble without qualification... > > izidor > > On Dec 8, 2005, at 11:03 AM, David Mitchell wrote: > >> Hello everyone, >> >> I''ve got the following scenario >> >> Controller >> @things = Thing.find_all >> @thing_pages, @things = paginate :things, :per_page => 10, :order_by >> => ''otherthings.otherthingname, thingname'', :joins => ['',otherthings >> where things.id = otherthings.thing_id''] >> >> View >> for thing in @things >> <%= thing.id %> >> <%= thing.thingfield1 %> >> >> What I''d expect to see is a list of the id fields for records in the >> **things** table; instead I''m seeing a list of the id fields for >> records in the **otherthings** table. However, ''thing.thingfield1'' >> gives exactly what I want - a list of the thingfield1 fields for >> records in the things table. >> >> It seems that the join in the controller code has picked up the id >> field from the otherthings table, rather than the id field from the >> things table. I''ve confirmed that the model definitions for both >> ''things'' and ''otherthings'' is correct. >> >> Has anyone else encountered this, and/or know of a way around it? >> I''m >> using this block of code all over the place (with different table >> names) in my app, and it''s working perfectly everywhere except this >> one place. >> >> Thanks in advance >> >> Dave M. >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks guys, that did the trick. Just curious: how did you track down that solution? The :select option isn''t covered in the API at http://rails.rubyonrails.com/classes/ActionController/Pagination.html which is where I was looking, so is there another source of info I should be searching as well? Or did you just walk through the code and find the option that way? Thanks again Dave M. On 12/9/05, Bruce Balmer <brucebalmer-ee4meeAH724@public.gmane.org> wrote:> Yes, I encountered it. The id for the second table has the same name > as the first and thus the first is ovewritten by the second. No > problem. You use :select=> and then when you select the id column of > the second table you change its name like > > secondtable.id as ''foo_id'' > > bruce > > > On 8-Dec-05, at 6:00 AM, Izidor Jerebic wrote: > > > > > You need to add :select => ''things.*'' to pagination options. This > > happens because there are more than one columns with name > > ''id'' (things.id and otherthings.id). Which column will prevail is > > quite a gamble without qualification... > > > > izidor > > > > On Dec 8, 2005, at 11:03 AM, David Mitchell wrote: > > > >> Hello everyone, > >> > >> I''ve got the following scenario > >> > >> Controller > >> @things = Thing.find_all > >> @thing_pages, @things = paginate :things, :per_page => 10, :order_by > >> => ''otherthings.otherthingname, thingname'', :joins => ['',otherthings > >> where things.id = otherthings.thing_id''] > >> > >> View > >> for thing in @things > >> <%= thing.id %> > >> <%= thing.thingfield1 %> > >> > >> What I''d expect to see is a list of the id fields for records in the > >> **things** table; instead I''m seeing a list of the id fields for > >> records in the **otherthings** table. However, ''thing.thingfield1'' > >> gives exactly what I want - a list of the thingfield1 fields for > >> records in the things table. > >> > >> It seems that the join in the controller code has picked up the id > >> field from the otherthings table, rather than the id field from the > >> things table. I''ve confirmed that the model definitions for both > >> ''things'' and ''otherthings'' is correct. > >> > >> Has anyone else encountered this, and/or know of a way around it? > >> I''m > >> using this block of code all over the place (with different table > >> names) in my app, and it''s working perfectly everywhere except this > >> one place. > >> > >> Thanks in advance > >> > >> Dave M. > >> _______________________________________________ > >> Rails mailing list > >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Well. I had the same problem, wrote to the list and got an answer from David Heinemeier Hansson. Very cool that he should bother to answer. So, what goes around comes around. I figured it was my turn to be helpful. bruce On 8-Dec-05, at 2:16 PM, David Mitchell wrote:> Thanks guys, that did the trick. > > Just curious: how did you track down that solution? The :select > option isn''t covered in the API at > http://rails.rubyonrails.com/classes/ActionController/Pagination.html > which is where I was looking, so is there another source of info I > should be searching as well? > > Or did you just walk through the code and find the option that way? > > Thanks again > > Dave M. > > On 12/9/05, Bruce Balmer <brucebalmer-ee4meeAH724@public.gmane.org> wrote: >> Yes, I encountered it. The id for the second table has the same name >> as the first and thus the first is ovewritten by the second. No >> problem. You use :select=> and then when you select the id column of >> the second table you change its name like >> >> secondtable.id as ''foo_id'' >> >> bruce >> >> >> On 8-Dec-05, at 6:00 AM, Izidor Jerebic wrote: >> >>> >>> You need to add :select => ''things.*'' to pagination options. This >>> happens because there are more than one columns with name >>> ''id'' (things.id and otherthings.id). Which column will prevail is >>> quite a gamble without qualification... >>> >>> izidor >>> >>> On Dec 8, 2005, at 11:03 AM, David Mitchell wrote: >>> >>>> Hello everyone, >>>> >>>> I''ve got the following scenario >>>> >>>> Controller >>>> @things = Thing.find_all >>>> @thing_pages, @things = paginate :things, :per_page => >>>> 10, :order_by >>>> => ''otherthings.otherthingname, thingname'', :joins => >>>> ['',otherthings >>>> where things.id = otherthings.thing_id''] >>>> >>>> View >>>> for thing in @things >>>> <%= thing.id %> >>>> <%= thing.thingfield1 %> >>>> >>>> What I''d expect to see is a list of the id fields for records in >>>> the >>>> **things** table; instead I''m seeing a list of the id fields for >>>> records in the **otherthings** table. However, ''thing.thingfield1'' >>>> gives exactly what I want - a list of the thingfield1 fields for >>>> records in the things table. >>>> >>>> It seems that the join in the controller code has picked up the id >>>> field from the otherthings table, rather than the id field from the >>>> things table. I''ve confirmed that the model definitions for both >>>> ''things'' and ''otherthings'' is correct. >>>> >>>> Has anyone else encountered this, and/or know of a way around it? >>>> I''m >>>> using this block of code all over the place (with different table >>>> names) in my app, and it''s working perfectly everywhere except this >>>> one place. >>>> >>>> Thanks in advance >>>> >>>> Dave M. >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails