On Sep 16, 11:34 pm, surge <[EMAIL PROTECTED]> wrote:
> Gotcha. I was thinking about playing with the :select option. That was
> my next step before I posted, but I decided to run it by the community
> first. :select is nice but you lose the convenience of the Rails
> automation, don't you? You can also forget to select an id column for
> example. Every new item would require a modification of the select
> clause... An SQL view would work but I'm still working with mysql
> 4.1 :) And I kind of want to keep as much as possible within the
> application. Feels right that way. Let me know if you think I'm
> missing something.
>
> Thank you for your response all the same...

You were partially correct about the relational listing concept. Rails
2.1's eager loading will use 2 SQL statements when you add
the :include => :issues option: 1 for the tickets table, and 1 for the
issues matching those tickets (in the form of a WHERE IN clause). It's
up to AR to compose those records to AR objects with 1-to-many
associations.

If you have a :condition which specifies something like

:condition => "issues.active = 1"

You get the scenario which you stated: a cartesian product of the
resultset. If you have 100 tickets with 100 issues each, you get
100x100 = 10,000 records, which AR has to compose into 100 AR objects
with a 1-to-many associations. It doesn't mean that the tickets will
be listed 100 times each, it's just that the resultset will return the
tickets 100 times each. But still, a cartesian product is not a very
good thing from an optimization point of view.

If you really need all the fields from the table, you can always use:

:select => "*, COUNT(...) AS issue_count"

Although there's a caveat with the :select approach: AFAIK, MySQL 4.1
does not support subqueries, so you might be stuck with the :include
option afterall.

Or you might want to look here:

http://railsexpress.de/blog/articles/2005/11/06/the-case-for-piggy-backed-attributes

HTH
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to