I think this is not possible to achieve, because the context in which proc is executed differs depending on what it is called. I made a basic application with your models, and this is what happens when you call various methods ( ap this ):
class School < ActiveRecord::Base has_many :students, :conditions => proc { ap self; "year_id=#{send(:active_year_id)}" } end
When you invoke student attitudes from a school instance, the proc context is that instance of School , so it responds to the active_year_id method
[31] pry(main)> School.first.students School Load (0.2ms) SELECT "schools".* FROM "schools" LIMIT 1 #<School:0x007fcc492a7e58> { :id => 1, :name => "My school", :active_year_id => 1, :year_id => 1, :created_at => Tue, 08 May 2012 20:15:19 UTC +00:00, :updated_at => Tue, 08 May 2012 20:15:19 UTC +00:00 } Student Load (0.2ms) SELECT "students".* FROM "students" WHERE "students"."school_id" = 1 AND (year_id=1) +----+----------------+-----------+---------+-------------------------+-------------------------+ | id | name | school_id | year_id | created_at | updated_at | +----+----------------+-----------+---------+-------------------------+-------------------------+ | 1 | My student | 1 | 1 | 2012-05-08 20:16:21 UTC | 2012-05-08 20:16:21 UTC | | 2 | Second student | 1 | 1 | 2012-05-08 20:18:35 UTC | 2012-05-08 20:18:35 UTC | +----+----------------+-----------+---------+-------------------------+-------------------------+ 2 rows in set
But when you call the include relation, the context is different and what proc gets as self is a Student class, so it does not respond to this method, and this will throw an error
[32] pry(main)> School.includes(:students).all School Load (0.3ms) SELECT "schools".* FROM "schools" class Student < ActiveRecord::Base { :id => :integer, :name => :string, :school_id => :integer, :year_id => :integer, :created_at => :datetime, :updated_at => :datetime } NoMethodError: undefined method `active_year_id' for #<Class:0x007fcc4a6a3420> from /Users/fabio/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
I think that the has_many relationship cannot be used with a kind of proc that relies on the instance method of the School instance. I think the only way to use procs as described here is to calculate some condition at runtime that does not include instance methods (temporary conditions, where with data from unrelated models, etc.).
In addition, School.includes(:students).all in my example cannot work, because it must call the active_year_id method for each instance of School (which must be extracted from db before inclusions can be evaluated) and thus , the effect of the intended behavior includes .
All this is true if active_year_id is a computed method defined in the School class based on instance data. Instead, if active_year_id not a method, but a field (db column) of the School class, you can play with join and scopes to achieve a result similar to what you want to achieve, but you need to code it manually.