Question

For example lets say you have:

       class Model < AR::Base 
         has_many :somethings, :finder_sql => "SELECT * FROM somethings"
       end

       class Something < AR::Base
         named_scope :valuable {...code...}
       end

       # Assume you have one model but 0 somethings:  
       # Model.first.somethings          # => [] Good!
       # Model.first.somethings.valuable # => nil Bad! Should return [] !!!

This only occurs when you have finder_sql in has_many relationship. In other cases it works as expected.

Is this normal behavior of Rails 2.3.14?

Was it helpful?

Solution

yes, if you specify finder_sql, then you will not be able to append scopes. that's because finder_sql is for situations that do not fit the normal activeRecord paradigm. That being said, the way you have this constructed is incorrect. you should not need to store a select * with no filters in a relationship like that. the purpose of a relationship is to apply a filter to the other model. so, the way you have it Model.find(params[:id]).somethings is exactly the same as calling Something.all . the scope will work for you in the latter case because .all can be scoped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top