Dec
3rd
Mon
3rd
finder_sql String gets eval'd later
If you ever need to use ActiveRecord’s has_many with finder_sql, remember that the key to finder_sql is that the String get eval’d in the context of the ActiveRecord instance rather than the ActiveRecord class. So make sure you use single quotes! This prevents the String from being interpolated too soon.
Do this:
has_many :foos,
:finder_sql => 'select ... where some_id=#{id}'
Not this:
has_many :foos,
:finder_sql => "select ... where some_id=#{id}"
In the former case, the #{id} will be the id of the ActiveRecord instance. In the latter case, the #{id} will be the id of the ActiveRecord class.