I am maintaining a Rails 2.3.x app. Part of it deals with Users and Roles. A user can have a number of roles, each either for some period or indefinitely. I'm trying to write a has_many extension to quickly find all roles that a user currently have active. It involves writing a has_many extension due to the logic of finding active role matching for current time.
So the models look like so:
class User
has_many :role_user_matchings
has_many :roles, :through => :role_user_matchings do
def active
find(:all, :conditions => ['matchings.starts_at <= ? AND (? < matchings.ends_at OR matchings.ends_at IS NULL)', Time.now, Time.now])
end
end
class RoleUserMatching
belongs_to :user
belongs_to :role
# has fields like starts_at and ends_at
# ends_at can be null, meaning indefinitely
end
class Role < AR:Base
has_many :role_user_matchings
has_many :users, :through => :role_user_matchings
end
I need to know currently active roles for the user every time something like this is executed:
@user.roles.active
As role assignments may change frequently or based on the time, I need to ensure roles are returned based on the current time.
So this appears to be working in development mode. I'm wondering if calls to Time.now above are cached in this situation in production mode? Would appreciate if someone could also explain why or why not. Thanks.
As a side note: I know that with named_scopes, to avoid caching, you need to use a lambda expression
# call to 1.week.ago will be cached and will remain the same until restart
named_scope :recent, { :conditions => ['created_at > ?', 1.week.ago] }
# 1.week.ago will not be cached first
named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
Wondering if same applies to association extensions.