Jon Seidel wrote: > I have a new app (Rails 2.3.8) that uses lots of money fields. I'm > using the money gem (3.0.5) and the acts_as_money plugin. I've written > a number of rspec model examples and everything seems to be working > fine there. > > My problem is in defining forms for new & edit. As I've done in past > projects for complex layouts, I extracted the basic form to a partial > and include it in the new & edit views. > > However, the model object is created and left with nulls in the > fields, and causes the money gem to complain with: > "undefined method `subunit_to_unit' for nil:NilClass". > > I thought I could use something like after_initialize() to hook into > the creation of a new object in Rails and set all the money attributes > to zero, but that didn't work (and several posts recommended against > that for performance reasons)... > > Any suggestions on a clean way to hook my model object and make sure > it's got zeros for all the money values when the new object is > instantiated?
Why not define a default value for the field on the DB side? This has the added advantage of ensuring that there is no way to accidentally create a record without these values even if the Rails app is bypassed. Best, -- Marnen Laibow-Koser http://www.maren.org mar...@marnen.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-t...@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.