Hello, I am trying to get specific fields returned from my query but I am not getting some of them. For example when I use User.joins(:books).select("users.email, users.fname, books.name").find(7) The generated SQL Query from above code is SELECT users.email, users.fname, books.name FROM users INNER JOIN book_users ON users.id = book_users.user_id INNER JOIN books ON books.id = book_users.book_id WHERE users.id = 7 LIMIT 1 Output received is - #<User email: "email1-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org", fname: "Arlene"> Rails doesn''t return the books.name even though the generated sql query has that an if I run the sql directly I get the three columns. I am on Rails 3.0.5 and Ruby 1.9.2p180. Is there any configuration setting to enable this or am I missing something here? Thanks, -S -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, You are getting all the fields it''s just that the console is only displaying the user object. try this: user = User.joins(:books).select("users.email, users.fname,books.name ").find(7) user.name You might want to take look at Hirb, it''s a mini view framework for your console(you will be able to view all fields in a nice formatted table) Hope it helps. -Jazmin On Thu, Jun 2, 2011 at 9:02 PM, skt <stibrewal-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I am trying to get specific fields returned from my query but I am not > getting some of them. > > For example when I use > > User.joins(:books).select("users.email, users.fname, > books.name").find(7) > > The generated SQL Query from above code is > > SELECT users.email, users.fname, books.name > FROM users > INNER JOIN book_users ON users.id = book_users.user_id > INNER JOIN books ON books.id = book_users.book_id > WHERE users.id = 7 > LIMIT 1 > > Output received is - > > #<User email: "email1-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org", fname: "Arlene"> > > Rails doesn''t return the books.name even though the generated sql > query has that an if I run the sql directly I get the three columns. > > I am on Rails 3.0.5 and Ruby 1.9.2p180. Is there any configuration > setting to enable this or am I missing something here? > > Thanks, > -S > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Jazmin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks Jazmin. I was wondering what was going on and wasn''t aware that console didn''t show all the values - it is very misleading if one isn''t aware of this. Thanks again. On Jun 2, 7:27 pm, Jazmin <jazminschroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > You are getting all the fields it''s just that the console is only displaying > the user object. > try this: > > user = User.joins(:books).select("users.email, users.fname,books.name > ").find(7) > user.name > > You might want to take look at Hirb, it''s a mini view framework for your > console(you will be able to view all fields in a nice formatted table) > > Hope it helps. > -Jazmin > > > > > > > > > > On Thu, Jun 2, 2011 at 9:02 PM, skt <stibre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hello, > > > I am trying to get specific fields returned from my query but I am not > > getting some of them. > > > For example when I use > > > User.joins(:books).select("users.email, users.fname, > > books.name").find(7) > > > The generated SQL Query from above code is > > > SELECT users.email, users.fname, books.name > > FROM users > > INNER JOIN book_users ON users.id = book_users.user_id > > INNER JOIN books ON books.id = book_users.book_id > > WHERE users.id = 7 > > LIMIT 1 > > > Output received is - > > > #<User email: "ema...-zuv13RyHHZkAvxtiuMwx3w@public.gmane.org", fname: "Arlene"> > > > Rails doesn''t return the books.name even though the generated sql > > query has that an if I run the sql directly I get the three columns. > > > I am on Rails 3.0.5 and Ruby 1.9.2p180. Is there any configuration > > setting to enable this or am I missing something here? > > > Thanks, > > -S > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > Jazmin-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.