Given the following ActiveRecord models:
class Tree < ActiveRecord::Base
# attributes: title, price, some_date
has_many :nodes
end
class Node < ActiveRecord::Base
# attributes: title, price, some_date
belongs_to :tree
end
A Tree has many nodes (1'000 - 2'000). For performance and other reasons I'd like to cache the price attribute in the Node instances. A node should always have the same price like its associated tree. Which would make calculations and querying easier and faster.
Are there any best practices on how to do this? Maybe even plugins?
What I've come up so far is:
class Tree
after_save do; nodes.each{|n| n.update_attribute :price, price} if price_changed?; end
end
This can become a bit cumbersome overtime, as I need many such cached attributes.