Maybe I've missed it in the docs, but is there a simple way to *avoid* the combining behavior introduced in the fix for #1077:

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1077

Essentially, chaining named_scopes that use :joins will merge them by default. But sometimes that isn't what's intended. An example:

Two models: User and MembershipPayment

MembershipPayment belongs_to user, and has a field named 'year'.

On User:
named_scope :paid_for_year, lambda { |y| { :joins => :membership_payments, :conditions => { :membership_payments => { :year => y.to_s } } } }

This query:
User.paid_for_year(2008).paid_for_year(2009)

gives no results (since the generated SQL only joins membership_payments once, and the 'year' field can't have two values for the same record...). The *idea* of the query was to find users with payments for both 2008 and 2009.

I can get this working by manually aliasing the table name in the :joins clause:

named_scope :paid_for_year, lambda { |y| {
:joins => "INNER JOIN membership_payments AS `membership_payments_#{y}` ON `membership_payments_#{y}`.user_id = users.id",
  :conditions => ["`membership_payments_#{y}`.year = ?", y.to_s] } }

But this seems remarkably un-Railslike. Ideally, there would be another option (:exclusive_joins?) that specifies this type of aliasing behavior instead of the default. Thoughts?

I haven't spent enough time with Rails 3 to know - does ARel handle this automatically?

Thanks!

--Matt Jones


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