Hi guys, When I do: temp = User.find(:all, :joins => "INNER JOIN contents ON users.agent_id contents.id", :select => "contents.id, users.id, users.u_date") temp.first.inspect It seems to work fine, but it only returns: #<User id: 6, u_date: "2009-10-03 19:32:23"> but not the contents.id information. I don''t get it, and I don''t know what to do to rectify this. It seems like since I call find on User, it only returns infos related to this model. By the way, when I copy the SQL code generated by Rails directly in my DB, it works fine and returns all 3 fields. Do you have any idea what I am missing? Thanks a lot! Peter
anton effendi
2009-Oct-05 03:06 UTC
Re: Problem using ActiveRecord find with :joins and :select
Hiii Active record --> mapping rec on table to object SO.... temp.first.inspect --> *user object* please try...... temp.first.content.inspect---> will show contents.id Note: first you must add relation between model user and model content.... Thank you On Mon, Oct 5, 2009 at 6:19 AM, PierreW <wamrewam-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Hi guys, > > When I do: > > temp = User.find(:all, > :joins => "INNER JOIN contents ON users.agent_id > contents.id", > :select => "contents.id, users.id, users.u_date") > > temp.first.inspect > > It seems to work fine, but it only returns: > #<User id: 6, u_date: "2009-10-03 19:32:23"> > > but not the contents.id information. > > I don''t get it, and I don''t know what to do to rectify this. It seems > like since I call find on User, it only returns infos related to this > model. > By the way, when I copy the SQL code generated by Rails directly in my > DB, it works fine and returns all 3 fields. > > Do you have any idea what I am missing? > > Thanks a lot! > Peter > > >-- Senior Rails Developer Anton Effendi - Wu You Duan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Oct-05 07:37 UTC
Re: Problem using ActiveRecord find with :joins and :select
On Oct 5, 12:19 am, PierreW <wamre...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Hi guys, > > When I do: > > temp = User.find(:all, > :joins => "INNER JOIN contents ON users.agent_id > contents.id", > :select => "contents.id, users.id, users.u_date") > > temp.first.inspect > > It seems to work fine, but it only returns: > #<User id: 6, u_date: "2009-10-03 19:32:23"> > > but not the contents.id information. > > I don''t get it, and I don''t know what to do to rectify this. It seems > like since I call find on User, it only returns infos related to this > model. > By the way, when I copy the SQL code generated by Rails directly in my > DB, it works fine and returns all 3 fields. > > Do you have any idea what I am missing?attributes are stored in a hash, ie there can be only one attribute called id - you need to alias the contents.id attribute. Even when you do that, inspect won''t show the extra value, but temp.first.content_id (assuming you alias it as content_id) should return the right id. Lastly, what''s the point of the hoin to get contents.id when you know the contents.id == users.agent_id ? Fred> > Thanks a lot! > Peter
Thanks a lot guys for clarifying this. It worked with the aliases as you suggested. Fred, to your last point: sorry about that, you are right. I just made up the example to show the mechanics, I didn''t realize it didn''t quite make any sense. Peter On Oct 5, 8:37 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Oct 5, 12:19 am, PierreW <wamre...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > > > > Hi guys, > > > When I do: > > > temp = User.find(:all, > > :joins => "INNER JOIN contents ON users.agent_id > > contents.id", > > :select=> "contents.id, users.id, users.u_date") > > > temp.first.inspect > > > It seems to work fine, but it only returns: > > #<User id: 6, u_date: "2009-10-03 19:32:23"> > > > but not the contents.id information. > > > I don''t get it, and I don''t know what to do to rectify this. It seems > > like since I call find on User, it only returns infos related to this > > model. > > By the way, when I copy the SQL code generated by Rails directly in my > > DB, it works fine and returns all 3 fields. > > > Do you have any idea what I am missing? > > attributes are stored in a hash, ie there can be only one attribute > called id - you need to alias the contents.id attribute. Even when you > do that, inspect won''t show the extra value, but temp.first.content_id > (assuming you alias it as content_id) should return the right id. > Lastly, what''s the point of the hoin to get contents.id when you know > the contents.id == users.agent_id ? > > Fred > > > > > > > Thanks a lot! > > Peter