Rails activerecord includes. How to access the included columns?

Posted by Lee Quarella on Stack Overflow See other posts from Stack Overflow or by Lee Quarella
Published on 2012-06-28T15:14:27Z Indexed on 2012/06/28 15:15 UTC
Read the original article Hit count: 142

Filed under:
|

I my User has_many :event_patrons and EventPatron belongs_to :user. I would like to slap together the user with one specific event patron with something like this sql statement:

SELECT * FROM `users` INNER JOIN `event_patrons` ON `event_patrons`.`user_id` = `users`.`id` WHERE `event_patrons`.`event_id` = 1

So in rails I tried this:

User.all(:joins => :event_patrons, :condidions => {:event_patrons => {:event_id => 1}})

But that gives me SELECT users.* instead of SELECT *:

SELECT `users`* FROM `users` INNER JOIN `event_patrons` ON `event_patrons`.`user_id` = `users`.`id` WHERE `event_patrons`.`event_id` = 1

I then tried to switch the :joins with :include and got a whole jumbled mess that still returned me only the columns in User and none from EventPatron.

What am I missing?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord