On 7/4/07, Tarmo Tänav <[EMAIL PROTECTED]> wrote:
>
>
> This is an issue also described in rails ticket #6924 [1]
>
> Simple :conditions on associations work very well when associations
> are loaded separately, but they don't work so well when eager
> loading is used and either several tables use the same field name
> or the more extreme case of the same table appearing more than
> once in the SQL (self-referential associations).
>
> The first case can be worked around by just refering to the table
> attributes in :conditions through their table name, and ActiveRecord
> even does it automatically when the conditions are specified
> as a hash.
>
> In the second case the workarounds are more limited, it is possible
> to find out how activerecord aliases a table in a specific case so
> in the :conditions you can use that alias but this association would
> only work in that one specific case, it can not be eager loaded in
> any way that would produce a different alias and whats worse it
> can not be loaded in the normal way (without eager loading).
>
> It's clear that not all association options can be supported for
> eager loading so one could argue that :conditions being limited
> in this way is fine but I think this could be fixed.
>
> What is needed is a way to refer to the table of the current
> association in :conditions in a way that allows ActiveRecord to
> replace those references with the real table name or alias. In case
> of conditions as a hash this would require no syntax changes at
> all but in other cases it would require a special marker, like the
> one used for named or positional bind
> variables (":table", ":current_table", ":association"...).
>
> Do you think this should be fixed?
> Have I described a solution that would be acceptable?
>
>
> Thanks in advance for any responses,
>
> [1] http://dev.rubyonrails.org/ticket/6924



I've been thinking about this issue myself and have thought of how I would
like to use it, but I have not worked out how I could patch it.  I hope I'm
not way off the conversation here. ;)

Basically I have been thinking that if a hash is passed to the conditions
option whose keys are either the current table, or one of the included
associations then the conditions for that association should be scoped
appropriately

eg.

User.find( :all, :include => :articles, :conditions => { :users    => {
:active => true } ,

:articles => {  :active => true,

:published => true } } )

If the conditions hash is an array or does not include keys for active
associations then it should behave as current.
User.find( :all, :include => :articles, :conditions => { :active => true } )
#=> Should be scoped to user

Is this inline with what your saying?  Sorry if it's not.

Daniel

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

Reply via email to