views:

314

answers:

1

In a class A I have two scopes, s1 and s2 which both join over a table T using the exact same join columns:

named_scope :s1 :joins => "JOIN T on T.id = A.t_id", ...some conditions
named_scope :s2 :joins => "JOIN T on T.id = A.t_id", ...some other conditions

Now, doing this fails:

A.s1.s2.all

Error:

ActiveRecord::StatementInvalid: Mysql::Error: Not unique table/alias: 'T'

I sort of expected Rails to be smart about those identical joins, and simply apply the join once, but it doesn't. I could use table aliases, but then I'd still have two identical joins for no good reason.

I'm sure there must be a proper solution to this?

+1  A: 

Turned out this is a bug in Rails 2.3.4:

In my actual code I used different character case for the ON keyword in the JOIN expression, and Rails wasn't smart enough to merge that. Apparently scopes do case sensitive comparison on JOIN strings.

I opened a bug report: link text

Matthias