I've run into problems trying to anticipate the aliased table name as
well. I think Rails should have a more consistent way to refer to
aliased table names. Perhaps a parameter to the association
definition?
class Employee < AR::Base
...
has_many :subordinates, :class_name => 'Employee', :sql_alias =>
'subordinates', :conditions => 'subordinates.active is true'
has_one :boss, :class_name => 'Employee', :sql_alias =>
'bosses', :conditions => 'bosses.active is true'
...
end
On Jul 4, 6:39 am, 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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---