add method to reflection-object and named-scopes
Posted
by toy
on Stack Overflow
See other posts from Stack Overflow
or by toy
Published on 2010-02-20T17:02:32Z
Indexed on
2010/04/03
3:03 UTC
Read the original article
Hit count: 324
ruby-on-rails
|activerecord
I Like to add a method to my has_many relation in the way that it is applyed on the relation object.
I got an Order wich :has_many line_items
I like to write things like
order.line_items.calculate_total # returns the sum of line_items
this I could do with:
:has_many line_items do
def calculate_total
...
end
end
but this would not be applyed to named_scopes like payalbes_only:
order.line_items.payables_only.calculate_total
here calculate total would receive all line_items of order and not the scoped ones from payables_only-scope. My log tells me that the paybles_only scope is even not applied to the sql.
© Stack Overflow or respective owner