I am maintaining an application that has an account model which has many
payments. So the account model has a has_many relationship as shown
class Account < ActiveRecord::Base
has_many :payments, :order => “created_at ASC” do
def charge(amount, paying_for)
Payment.charge(proxy_owner, amount, paying_for)
def commit(action, amount, paying_for) Payment.commit(proxy_owner, action, amount, paying_for) end def gift(days = nil) Payment.gift(proxy_owner, days) end
This may be an RoR 101 question, but why would someone define methods in
a has_many block? Can’t this be simply called as:
account_inst.payments.charge(amount, paying_for) for example?
I do not understand what is the benefit of defining (or proxying ) class
methods belonging to the many side of this relationship in this manner?
Thanks for your time.