rails/rails — Issue #3920: Documentation problem for :finder_sql in ActiveRecord::Associations::ClassMethods
Author: clst
Created: 2011-12-09 18:00:17+00:00
State: open

The documentation states this as an example

has_many :subscribers, :class_name => "Person", :finder_sql =>
'SELECT DISTINCT people.* ' +
'FROM people p, post_subscriptions ps ' +
'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
'ORDER BY p.first_name'

multiple problems:

people.* vs. people p alias will not work (on SQLite at least)

String vs. proc

Rails 3.1 requires a proc:

example:

has_many :posts, :finder_sql =>
proc {"SELECT p.* from posts p join topics t on p.topic_id = t.id where t.id=#{id}"}

That took me ages to figure out.