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"...).

You can already do what you want (assuming it hasn't changed since
1.2.3), but it is ugly:

  :conditions=>'#{"#{aliased_table_name}." rescue ""}attribute = value'

You need the rescue because aliased_table_name isn't available when
doing lazy loading (it used to be before Rails 1.2).

Jeremy

--~--~---------~--~----~------------~-------~--~----~
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