Does anyone know why ActiveRecord does not merge joins when using scopes ?

Scopes are wonderful, thanks for has_finder and named scopes, but joins aren’t supported. This code:

1 City.nearest(latitude, longitude).find_tagged_with(tags)

raises an ActiveRecord::StatementInvalid complaining that a specified column could not be found. The column can’t be found because the join was dropped on the floor, instead of being merged.

The change me and Marc-André are proposing to make is this:

 1 $ git diff 36236235039b3b0cab22a24ce7b45a8ec071cb5e 45e7c94be02a586f70908694545e8484e4a85382 vendor
 2 diff --git a/vendor/rails/activerecord/lib/active_record/base.rb b/vendor/rails/activerecord/lib/active_record/base.rb
 3 index 261d854..427e3ef 100755
 4 --- a/vendor/rails/activerecord/lib/active_record/base.rb
 5 +++ b/vendor/rails/activerecord/lib/active_record/base.rb
 6 @@ -1673,6 +1673,8 @@ module ActiveRecord #:nodoc:
 7                          hash[method][key] = [params[key], hash[method][key]].collect{ |sql| "( %s )" % sanitize_sql(sql) }.join(" AND "
 8                        elsif key == :include && merge
 9                          hash[method][key] = merge_includes(hash[method][key], params[key]).uniq
10 +                      elsif key == :joins && merge
11 +                        hash[method][key] = [params[key], hash[method][key]].join(" ")
12                        else
13                          hash[method][key] = hash[method][key] || params[key]
14                        end

This is based on Rails 2.0.2, but Rails Edge has the same problem. So, any ideas ?


Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend


Projects I work on

Projects I worked on