basic modification of default model output with scope
I find myself doing the same things over and over开发者_运维技巧 again just to make one small modification to standard model output. I have a series of tables that I store information about products, etc. and all of which store prices. The prices are stored in US dollars but the output depends on the currency the user wants which is stored in a their session.
Examples:
Product Detail Blah Price
Hammer Red More 5.00
Nail Blue Stuff 3.99
Is there a simple robust way to modify the output so that when i call:
Product.all
I could attach something like
Product.all.currency('EUR')
Product.find(22).currency('EUR')
Product.find(:all, :conditions => 'etc etc').currency('EUR')
or
Product.all.currency(0.69)
and simply multiply all of the items in the Price column? Could named_scope do this?
try reordering your chaining like:
Product.currency(0.69).all
I have not tested that, but you may have issue with other arbitrary conditions since your :select
contains *
Nevermind...
named_scope :currency, :select => '*, price * 0.63 AS price'
seemed to work except I can't chain it to normal finds as I would like.
精彩评论