Rails - Including associations with dynamic conditions

Viewed 6038

Given a school model and a student model with the school having a has_many relation to student:

has_many :students, :conditions => proc  { "year_id=#{send(:active_year_id)}" }

where active_year_id is a method defined in the school model, I'm encountering an error that "active_year_id is undefined" when calling:

School.where(:active => true).includes(:students)

The condition works fine when I do, say,

School.where(:id => 10).students

Only when I try to use includes do I get that error. Is that the right behavior. If not, what am I doing wrong and how do I fix ?

Using Rails 3.0.9, REE 1.8.7.

4 Answers
Related