Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11545 (new defect)

Opened 8 months ago

Last modified 8 months ago

Named scopes don't handle finder_sql associations

Reported by: Henrik N Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc: nkallen

Description

I haven't looked into how hard this would be to fix, but:

Named scopes don't handle :finder_sql associations well.

I've attached a failing test as a diff. The interesting bits:

class Author < ActiveRecord::Base
  has_many :posts
  has_many :posts_with_finder_sql, :class_name => "Post", :finder_sql => 'SELECT * FROM posts WHERE author_id = #{id}'
end

class Post < ActiveRecord::Base
  named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'"
end

# Sanity check
assert_equal authors(:david).posts, authors(:david).posts_with_finder_sql
# Currently fails: the latter returns nil
assert_equal authors(:david).posts.containing_the_letter_a,
             authors(:david).posts_with_finder_sql.containing_the_letter_a

Attachments

named_scopes_with_finder_sql_failing_test.diff (1.4 kB) - added by Henrik N on 04/07/08 14:35:12.

Change History

04/07/08 14:35:12 changed by Henrik N

  • attachment named_scopes_with_finder_sql_failing_test.diff added.

04/07/08 14:42:05 changed by Henrik N

I suppose short of a SQL parser a 100 % solution is unlikely. But returning nil is weird: there should probably be an exception ("Can't use named scopes with finder_sql").