2012-05-15 33 views
9

我使用的钱护栏宝石的Rails 3.2.3,我已经得到了它具有以下产品型号:弃用警告创建属性“货币”

我的模型

class Product < ActiveRecord::Base 
    attr_accessible :name, :price 

    composed_of :price, 
    :class_name => "Money", 
    :mapping => [%w(price_cents cents), %w(currency currency_as_string)], 
    :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }, 
    :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") } 


end 

我的测试

require 'spec_helper' 

describe Product do 
    context "testing money gem" do 
    it "creates product with price" do 
     product = Product.create(:price => 200) 
     product.price.should eq(200) 
     product.price_cents.should eq(20000) 
    end 
    end 
end 

弃用警告我收到了。

% rspec spec/models/product_spec.rb 

Product 
    testing money gem 
DEPRECATION WARNING: You're trying to create an attribute `currency'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in <top (required)> at /home/map7/project/spec/models/product_spec.rb:6) 
    creates product with price 

Finished in 0.06682 seconds 
1 example, 0 failures 

如何解决此弃用警告?

更新

如果我添加“货币”,以它开始工作表。我应该这样做吗?

回答

14

显然,在Rails的3.2及以上任意属性(属性不存储在数据库中)不再允许。似乎没有办法绕过它。

这里是犯了过时消息:https://github.com/rails/rails/commit/b2955edc 这里是为什么:https://github.com/rails/rails/commit/50d395f96ea05da1e02459688e94bff5872c307b

在你的情况price_cents和货币仍然需要存储在数据库中,然后您构成的一类会从那里。

+0

超级奇怪。根据宝石,我不需要在数据库中放入“货币”列,因为这是可选的。我注意到这只发生在我的FactoryGirl中,如果我试图将“货币”对象设置为“价格”列,但如果我更改为将“整数”美分设置为“price_cents”,它工作正常,不会消耗弃用警告。 –

1

增加了“货币:字符串”到我的模型