I have a languages
table that hardly every changes. I am trying to avoid database queries on this table other the initial caching.
class Language < ActiveRecord::Base
attr_accessible :code, :name, :native_name
def self.find_by_name_in_cache(name)
get_all_cached.find {|l| l.name == name}
end
def self.find_by_code_in_cache(code)
get_all_cached.find {|l| l.code == code}
end
def self.find_by_id_in_cache(id)
get_all_cached.find {|l| l.id == id}
end
def self.get_all_cached
Rails.cache.fetch('all_languages') {Language.all}
end
end
All goes fine as long as I am using one of the find_in_cache
methods that I have defined.
My question is, how can I force ActiveRelation
to use the caching as well.
For example, consider the following user model:
class User < ActiveRecord::Base
belongs_to :native_language, :class_name => :Language, :foreign_key => :native_language_id
end
When I access @user.native_language
, it queries language
from the database. I would highly appreciate any ideas to prevent this.
I know I can do the following:
class User < ActiveRecord::Base
belongs_to :native_language, :class_name => :Language, :foreign_key => :native_language_id
def native_language_cached
Language.find_by_id_in_cache(self.native_language_id)
end
end
However, I was hoping for a more transparent solution as a lot of my tables reference languages
table and it would be so messy to add the cached
methods to all these models.