On Wednesday 13 May 2009, Matt Jones wrote:
> Try it in a named_scope, thus:
>
> class Person < AR::Base
> named_scope :as, lambda { |role_name| { :joins => 'CROSS JOIN
> role_types', :conditions => ["(roles.role_type_id = role_types.id)
> AND role_types.name = ?", role_name] } }
> end
That works for queries, but AFAICT it is equivalent to what I'm already
doing. The drawback is that
Person.as('actor') doesn't work because the necessary join with roles is
missing. If I add that, movie.participants.as('actor') blows up because
there the added join is a duplicate.
> But I'm *almost* positive that that still won't be able to trigger
> the named_scope :create_scope magic.
No, it won't, simply because ActiveRecord has no way to figure out what
additional parameters to use to build the through model (Role).
As I'm staring at this stuff for some time now, I'm still surprised that
there doesn't seem to be a fairly generic way to add elements to a
has_many :through association with a non-trivial through-model. For me,
the point of has_many :through, as opposed to habtm, is that the
intervening model carries some weight apart from relating two other
models with each other.
> The other thought would be to
> return a custom subclass of AssociationCollection from your 'as'
> association extension. You'd probably need to override a few methods
> (<< especially) to take into account the source of the request (the
> argument passed to as).
I was thinking of returning a subclass of AR::NamedScope::Scope. I can't
say which would be better.
Michael
--
Michael Schuerig
mailto:[email protected]
http://www.schuerig.de/michael/
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---