Reputation: 89193
In my application, Annotations
are considered "accepted" if either:
My question is how to find all accepted explanations with a single DB query. Basically I'm looking for the database-driven version of
Annotation.all.select do |a|
a.last_updated_by.roles.map(&:name).include?('editor') or a.state == 'accepted'
end
My first attempt was
Annotation.all(:joins => {:last_updated_by => :roles}, :conditions => ['roles.name = ? or annotations.state = ?', 'editor', 'accepted'])
But this returns a bunch of duplicate records (adding a .uniq
makes it work though)
Changing :joins
to :include
works, but this makes the query way too slow
Upvotes: 1
Views: 66
Reputation: 228
Are the results of your first attempt just wrong or do they only need an ".uniq"? Have you tried
:include => {:last_updated_by => [:roles]}
instead of the join?
or making two queries
@ids = Editor.all(:conditions => ["role = 'editor'"], :select => ["id"]).map{|e|e.id}
Annotation.all(:conditions => ["last_updated_by in (?) or state = ?", @ids.join(","), "accepted"]
is that any faster?
Upvotes: 1