On Friday, August 10, 2012 5:50:58 PM UTC-3, EMoreth wrote:
>
> Try this:
>
> Project.joins(:services).where(:services => { :id => [1,2]
> }).group(:id).having("count(*) = 2").all
>
> This produces to me:
>
> "SELECT `projects`.* FROM `projects` INNER JOIN `services` ON
> `services`.`project_id` = `projects`.`id` WHERE `services`.`id` IN (1, 2)
> GROUP BY id HAVING count(*) = 2"
>
> Which returns me only the projects who are associated to both services 1
> and 2.
>
Indeed that does the trick. Nice thinking. It's probably even better than
the intersection approach from SQL point of view.
Anyway, the reason we don't have & and | operators in activerecord is that
it's believed that there's always a better way to write a query not using
them?
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/rwPktzz3Kt8J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.