Thanks for answering.
Am 16.05.2007 um 01:50 schrieb dasil003:
It’s correct because there’s no other reasonable way for it to
behave. “Fixing” this would add a TON of complexity to ActiveRecord
and would result in a greater number of things breaking in the
opposite fashion.
Would it? The more I’m looking into this the more I’m wondering what
ActiveRecord does here.
In the logs I’m seeing something like this (simplyfied):
Article Load IDs For Limited Eager Loading (0.004604)
SELECT DISTINCT contents.id FROM contents
LEFT OUTER JOIN taggings ON (taggings.taggable_id = contents.id AND
taggings.taggable_type = ‘Content’)
LEFT OUTER JOIN tags ON tags.id = taggings.tag_id
WHERE tags.name IN (‘tag’)) AND ( (contents.type
= ‘Article’ ) )
ORDER BY contents.published_at DESC LIMIT 15
Article Load Including Associations (0.004464)
SELECT * FROM contents
LEFT OUTER JOIN taggings ON (taggings.taggable_id = contents.id AND
taggings.taggable_type = ‘Content’)
LEFT OUTER JOIN tags ON tags.id = taggings.tag_id
WHERE tags.name IN (‘tag’)) AND contents.id IN (‘972’, ‘971’, ‘448’,
‘181’)
ORDER BY contents.published_at DESC
That is, these queries are practically identical besides that the
first query fetches the ids that are used in the second query.
The :condition => “tags.name in (…)” part is used in both queries.
Why? This doesn’t seem to make sense to me. I guess this is some kind
of edge case and the same behaviour would make sense in other cases
that are treated the same by ActiveRecord?
The answer is yes, you have to write your own JOINs, but they aren’t
exactly monstruous.
Sorry for the wording
I just ment the amount of characters, like here: http://
pastie.caboo.se/59436
If you have the id of the tag it’s a single join
like this:
“INNER JOIN taggings ON taggings.article_id = articles.id AND
taggings.tag_id = #{id}”
Then you drop the condition.
Yes, similar to what ActiveRecord does internally, too. But
ActiveRecord doesn’t “drop the condition”. Shouldn’t it? (If it would
my problem with Mephisto’s tagging pages was solved.)
Also, very important, this won’t currently work if you add a :limit
clause because the “eager loading of associations with limit” query
that is run to fetch the IDs will drop the JOINs and then the final
query will use a list of IDs that doesn’t meet the criteria, therefore
resulting in fewer than your requested number of results.
As far as I can see from the log (see above) no JOINs are dropped (in
this case)? Also, the :limit is only applied to the first query
(“Article Load IDs For Limited Eager Loading”), so this theoretically
should work (in this case)?
–
sven fuchs fon: +49 (58 45) 98 89 58
artweb design fax: +49 (58 45) 98 89 57
breite straße 65 www: http://www.artweb-design.de
de-29468 bergen mail: [email protected]