I ended up going with your suggestion, Bryan.  Thanks!  I initially
had trouble figuring out where to put the type definition as it's not
mentioned in any of the documentation that I could find.  I ended up
putting it in config/initializers/cents.rb, and I had to change it
from class HoboType::Cents to class Hobo::Cents, but it seems to be
working great now.

Brian


On Feb 1, 11:40 am, Brian Corbin <[email protected]> wrote:
> I think this is the fix.  Added to Product and ProductOrders:
>
>   def initialize(*params)
>     super(*params)
>         self.price = Money.new(cents, currency)
>   end
>
> Maybe I should be doing this with the after_initialize callback
> instead, but this seems to work.
>
> Brian
>
> On Feb 1, 11:15 am, Bryan Larsen <[email protected]> wrote:
>
>
>
> > David Mathers contributed this very nice Cents rich type:
>
> >http://gist.github.com/127101
>
> > I'm supposed to get it integrated into hobo-contrib.  We finally added
> > the capability to load rich types from a plugin, so I don't have any
> > excuse any longer.
>
> > I'm sure the money gem could be made to work, but I'm sure it's easier
> > to code up a Money model from scratch using all the power that Hobo
> > gives you.
>
> > Bryan
>
> > Brian Corbin wrote:
> > > I was looking to add price information to the products in my
> > > application.  I may have to support other currencies besides USD in
> > > the future, so I was looking at the Money gem 
> > > fromhttp://money.rubyforge.org/.
> > > I'm pretty sure I've correctly added the code to my application;
> > > here's what I've done:
>
> > > I added a cents and currency field to my Product class, and a
> > > composed_of helper for price:
> > > class Product < ActiveRecord::Base
> > > ...
> > >   fields do
> > >        ...
> > >    cents          :integer, :default => 0
> > >    currency      :string, :default => "USD"
> > >   end
> > > composed_of :price, :class_name => "Money", :mapping => [ %w(cents,
> > > cents), %w(currency, currency) ]
>
> > > My Product class has some relations:
> > >   has_many :product_orders
> > >   has_many :orders, :through => :product_orders, :accessible => false
>
> > > Now, I am thinking that the price of a Product can change depending on
> > > several factors, and so the prices attached to the Product model are
> > > just the default price you get when creating an Order.  So the
> > > ProductOrder join table also needs to have price information as well.
>
> > > class ProductOrder < ActiveRecord::Base
> > > ...
> > >   fields do
> > >     quantity :integer, :default => 1
> > >     cents   :integer, :default => 0
> > >     currency :string, :default => "USD"
> > >   end
>
> > >   belongs_to :product
> > >   belongs_to :order
> > >   composed_of :price, :class_name => "Money", :mapping => [ %w(cents,
> > > cents), %w(currency, currency) ]
> > > end
>
> > > I've also added these methods to both the Product and ProductOrders
> > > models:
>
> > >   def formatted_price
> > >     self.price.format(:html)
> > >   end
>
> > >   def formatted_price=(price)
> > >     self.price = Money.new(price)
> > >   end
>
> > > I run the migration and it adds the cents and currency fields to the
> > > DB.  When I fire up the application server, I get an error on the New
> > > Order page:
>
> > > NoMethodError in OrdersController#new
>
> > > undefined method `round' for nil:NilClass
>
> > > The full trace is here:http://pastebin.ca/1774588
>
> > > The trace shows the error is occurring when it gets to the field-list
> > > for Order, which includes products, and product_orders.
>
> > > So my questions are:  What is calling the round method, and what's it
> > > being called on?  How do I get my app to use the formatted_price
> > > methods instead of the price, cents, or currency fields?  Should I
> > > pull out all this stuff and just use doubles and worry about other
> > > currencies if/when that problem comes up? :)   Thanks!

-- 
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en.

Reply via email to