[Sorry for the poor subject; this is really hard to explain]
I’m trying to run a search over several objects which themselves contain
multiple model objects, like so:
class Collection << ActiveRecord::Base
def findpkg
self.repositories.each do |r|
p = r.packages.find(:all, :conditions => “name like ‘%pkg%’”)
end
end
end
Unfortunately, AR proceeds to generate SQL that looks like this:
Package Load (0.000548) SELECT * FROM packages WHERE
(packages.repository_id = 3 AND (name like ‘%pkg%’))
Package Load (0.000167) SELECT * FROM packages WHERE
(packages.repository_id = 4 AND (packages.repository_id = 3
AND (name like ‘%pkg%’)))
Package Load (0.000151) SELECT * FROM packages WHERE
(packages.repository_id = 5 AND (packages.repository_id = 4
AND (packages.repository_id = 3 AND (name like ‘%pkg%’))))
Package Load (0.000160) SELECT * FROM packages WHERE
(packages.repository_id = 13 AND (packages.repository_id = 5
AND (packages.repository_id = 4 AND (packages.repository_id = 3
AND (name like ‘%pkg%’)))))
Note that each successive query prepends an extra clause, instead of
replacing the previous repository_id clause.
Has anyone seen anything like this? I can provide relevant chunks of
code
on request, but I’m not doing anything exciting in my models – just
standard sorts of has_many relationships. I’ll turn this into a bug
report
if needed, I just want to get a reality check before I go nuts.
- Matt