Reputation: 2622
When I add a attr_accessor to my model without the column in the database, I can add temporary data to an array of class objects.
My example :
class User < ActiveRecord::Base
attr_accessor :score
end
The problem however is that if cache in memcache an array of users with scores, the array goes from :
[< User >, < User >, < User >]
to:
[< User >, :@score, 100, < User >, :@score, 200, < User >, :@score, 300]
Is there any way to cache this information without breaking the array ?
EDIT : As requested, the actual code that puts the data in the cache :
def users_scoreboard
Rails.cache.fetch("special_scoreboard_#{self.cache_key}", :expires_in => 1.hour) do
users = Photo.missions(self.missions).created(self.start_at, self.end_at).map(&:user).uniq!
users = [] if users.nil?
users.each do |u|
u.score = u.score_for_special(self)
end
users.sort! { |a,b| b.score <=> a.score }
end
end
EDIT : What I'm using :
ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.1]
rails -v
Rails 3.2.0
memcached -i
memcached 1.4.5
memcached -i
memcached 1.4.5
gem -v dalli
1.8.8
But the problem appears either with Memcache or with the filestore.
Thanks for the help !
Upvotes: 3
Views: 1288
Reputation: 173
Try using cattr_accessor, it'll add the accessor method onto the class as well. This worked for me.
Upvotes: 0