I have a simple singleton cache that stores ActiveRecord model objects
for me so I’m not constantly hitting the DB for static data. This was
working great until I tried to add a method (any method really) to the
model, e.g:
class Tag << ActiveRecord::Base
def is_main?
logger.debug(“is_main?”)
return some_attribute && some_other_attribute
end
end
This method works fine when called from a unit test:
def test_is_main
tags = Tag.find_menus("__none")
tags.each {|t|
# Hit an accessor works fine
assert t.is_menu?
# Hit my method works fine
assert t.is_main?
}
end
But if I pull the tag from the cache, the attributes are all there, but
the is_main? call gives method_missing, eg:
def index
home_page = MenuCache.home_page
home_page2 = Tag.find_by_id(14)
logger.debug(“ConvsController.index(#{home_page.inspect})”)
logger.debug(“ConvsController.index(#{home_page2.inspect})”)
home_page2.is_main?
home_page.is_main # <= line #37
end
Yields (before logging edited):
Tag Load (0.010000) SELECT * FROM tags WHERE (tags.id
= 14 )
LIMIT 1
ConvsController.index(#<Tag:0x6a69f30 @attributes={“name”=>“Home”,
“updated_at”=>“2006-08-19 15:11:56”, “a_key”=>"__home",
“parent_menu”=>"__none", “created_by”=>“1”, “a_controller”=>"/pages",
“is_login_needed”=>“0”, “id”=>“14”, “list_view”=>“no_pref”,
“show_view”=>“no_pref”, “an_action”=>“index”, “deletable”=>“0”,
“inherit_security_from”=>nil, “ordering”=>“root_id DESC, lft”,
“position”=>“1”, “is_menu”=>“1”, “created_at”=>“2006-08-19 15:11:56”}>)
ConvsController.index(#<Tag:0x6796150 @attributes={“name”=>“Home”,
“updated_at”=>“2006-08-19 15:11:56”, “a_key”=>"__home",
“parent_menu”=>"__none", “created_by”=>“1”, “a_controller”=>"/pages",
“is_login_needed”=>“0”, “id”=>“14”, “list_view”=>“no_pref”,
“show_view”=>“no_pref”, “an_action”=>“index”, “deletable”=>“0”,
“inherit_security_from”=>nil, “ordering”=>“root_id DESC, lft”,
“position”=>“1”, “is_menu”=>“1”, “created_at”=>“2006-08-19 15:11:56”}>)
is_main?()
NoMethodError (undefined method `is_main?’ for #Tag:0x6a69f30):
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1792:in
method_missing' /app/controllers/pages_controller.rb:37:in
index’
So where did my method disappear to?
Thanks in advance, Brittain