I have a pretty simple join that I'm trying to do in a DB-friendly way.
Here are my models:
class Issuance
include DataMapper::Resource
property :id, Serial
property :issue_id, Integer
property :url_id, Integer
belongs_to :issue, :child_key => [:issue_id]
belongs_to :url, :child_key => [:url_id]
end
class Url
include DataMapper::Resource
property :id, Serial
property :host, String
[...]
has n, :issuances
has n, :issues, :through => :issuances
end
I want a list of all the Issuance models whose Urls belong to a certain
domain. I've tried the following statements:
Issuance.all(:url => {:host => "www.datamapper.org"})
Url.all(:host => "www.datamapper.org").issuances
In each of these cases, datamapper is doing it MOSTLY correct, by issuing
these queries:
SELECT `id` FROM `urls` WHERE `host` = 'www.datamapper.org'
SELECT `id`, `issue_id`, `url_id`, `host`, `found` FROM `issuances` WHERE
> `url_id` IN (1, 2, 3, 4, etc...)
I have lots of URLs (thousands) in the DB, so the "AND `id` IN (1, 2, 3...)"
part lists all the url_ids who are on www.datamapper.org host.
I appreciate DM using the IN() operator to avoid the N+1 problem, but in
this case what I really want it to do is issue only 1 SQL query and JOIN, to
the effect of:
SELECT * FROM issuances LEFT JOIN urls ON urls.id = issuances.url_id WHERE
> urls.host = 'www.datamapper.org' LIMIT 10;
Is that possible or is this a feature for future releases?
-J
--
You received this message because you are subscribed to the Google Groups
"DataMapper" group.
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/datamapper?hl=en.