they will all be incomprehensible the next day (I expect).
how class Person def self.find_by_relation_ids(*relation_ids) find(:all, :select => 'people.title, people.id', :joins => 'INNER JOIN people_relations ON people_relations.person_id = people.id', :conditions => ["people_relations.relation_id IN (?)", relation_ids], :group => "people.id, people.title HAVING count(*) = #{relation_ids.length}") end end would become incomprehensible next day?
people_arrays = users_array.each do |n| people_arrays << Relation.find(n).people end
This gives people_arrays as an array of arrays of people. Now the problem is which people are in each sub-array which is just the intersection of arrays.
i thought we always solved the problem, but you've got another one. check your direction then
Less efficient than doing everything in one big SQL perhaps, but much more readable and maintainable.
while agree on efficiency, let me disagree on readability. do you really think the Person#find_by_relation_ids is less readable, than quite a lot of lines of ruby code?
Alternatively, get all the relations in one go:
relation_array = Relation.find(:all, :conditions => "relations.id in (#{users_array.join ','})", :include => :people) unless users_array.blank?
Now you have everything in memory that you need.
even more, than you need now you have in memory often fitting tight. don't think i'm in premature optimisation and stuff. It's just one more sliiiight tradeoff for nothing.