Hi, Everyone
I am working on a custom accessor method like an example below:
class Forest < ActiveRecord : Base
has_many :trees
def total_water_usage
# summarize each tree's water_usage in this forest.
# return as string
end
end
class Tree < ActiveRecord : Base
belongs_to :forest
end
That is, I need your help for 2 questions:
How can I access each tree just for an instance of Forest class. (Like example below, the total water usage shouldn't summarize another forest's tree)
asiaForest = Forest.find_by_name( 'asia' ) asiaForest.total_water_usage
How can I force this method to be rendered by to_xml method? for example, I think the result should be similar to this:
asiaForest.to_xml <asiaForest> ... <total_water_usage>239000</total_water_usage> ... </asiaForest>
Could you help me to do this?