Please consider two objects of class Product:
class Product < ActiveRecord::Base {
:id => :integer,
:name => :string,
:total_stock => :integer,
:cents_retail_sale_amount => :integer,
:cents_cost_amount => :integer,
:created_at => :datetime,
:updated_at => :datetime
}
Let’s say I have 2 objects:
a{
:id => 12,
:name => "Shirt",
:total_stock => 1,
:cents_retail_sale_amount => 2500,
:cents_cost_amount => 2100,
:created_at => Mon, 23 Jul 2012 16:16:33 CDT -05:00,
:updated_at => Mon, 23 Jul 2012 17:24:15 CDT -05:00}
b {
:id => nil,
:name => "Polo Shirt",
:total_stock => 1,
:cents_retail_sale_amount => 2500,
:cents_cost_amount => 2100,
:created_at => Mon, 23 Jul 2012 16:16:33 CDT -05:00,
:updated_at => Mon, 23 Jul 2012 17:24:15 CDT -05:00}
Now here is what I’m trying to do:
a.update_attributes(b.to_product.attributes.except("id", "created_at", "updated_at")
The problem however ( which I caught only cause I have a validation on the cents fields – to allow amounts only between 0 and $100 ( 10000 cents)), is this – my cents fields get also updated and get extra 00 at the end – so I get $2500 & $2100 for the the retail and cost amounts. While really nice profit margin – not really realistic.
I can obviously do some kind of processing on the b object before updating attributes ( such as not updating money fields and do the separately one by one), but that’s not really efficient. So I’m wondering if I’m doing something wrong
Thanks
I switched to https://github.com/RubyMoney/money-rails gem instead which solved the issue