How can I write this query in Ruby on Rails to select includes model attributes like:
Post.includes(:comment).select(:name, :title, :comments => [:email, :text])
Asked
Active
Viewed 100 times
-2
Nikhil Goel
- 7
- 5
Harikesh Kolekar
- 1
- 1
-
This approach should work for you: https://stackoverflow.com/a/12403327/5448461 – Nikhil Goel Aug 21 '18 at 10:09
1 Answers
0
ActiveRecord#select doesn't support hashes as arguments, but you could use syntax like this:
Post.joins(:comment).select(:name, :title).merge(Comment.select(:email, :text))
But keep in mind that if Post has 3 comments it will return 3 instances of Post each with different comment data.
P. Boro
- 716
- 3
- 12