I’m working on a rails app where the associations between data change with time. I’ve created a model for the associations:
create_table :accounts_numbers do |t|
t.integer :number_id
t.integer :account_id
t.date :start_date
t.date :end_date
And, so far, I have a simple model
class Account < ActiveRecord::Base
has_and_belongs_to_many :numbers, :through => accounts_numbers
end
But instead of
@account.numbers
I need something like
@account.numbers_at(Date.new(2010,2,3))
I thought I could use :conditions, but I wouldn’t haven’t seen a way to tell has_and_belongs_to_many to create a parameterized field. I’ve also looked into named_scope, but that only seems to return accounts, not numbers.
More importantly, this pattern is going to cover many relationships in my code, so would there be a way to coin a time_dependent_has_and_belongs_to_many for use all over?
After much more searching, I finally found out what to do; In the
/libdir of my project, I created a module,TimeDependent:So, my model becomes
Which allows me to do exactly what I want: