I have models like
class Employee < ActiveRecord::Base belongs_to :branch, :foreign_key => :branch_code, :primary_key => :branch_code, :conditions => proc{["? BETWEEN enabled_day AND expiration_day", Date.current]} end class Branch < ActiveRecord::Base has_many :employees, :foreign_key => :branch_code, :primary_key => :branch_code scope :valid, lambda {where(["? BETWEEN enabled_day AND expiration_day", Date.current])} end
the employee belongs to the branch (simple), but the branch has several entries of the same identifier branch_code, and the one that is "valid at the moment" should always be used.
(as you can guess, the project migrates the old application, and it succeeds in the old scheme)
Now it works, but I have to write the same thing when the condition is twice (in fact, the branch is associated with a large number of tables, so 5 or 6 times).
I wonder if I can use the Branch scope as an association condition or any other DRY method?
source share