I'm trying to eager load relationship data via the Sequel gem's eager
method while using with_sql
to define the sql used for the query (it's a fairly complex query). The eager loading doesn't work at all. Instead it produces a single query per relationship for each row returned, which is what I'm trying to avoid. Here's some code:
class Foo
many_to_one :rel1
one_to_many :rel2
def self.bar
sql = 'some complex sql'
Foo.with_sql(sql).eager(:rel1, :rel2)
end
end
The problem is that when I call Foo.bar, the eager loading of rel1 and rel2 are seemingly ignored. I've verified this by watching the log - for each row returned by the with_sql
call, it generates 2 more queries - one for rel1 and one for rel2.
It may be worth noting that sql
contains 3 joins.
Is this a limitation of using with_sql
. Is there a way around this limitation?