2012-05-15 32 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

增加了「貨幣:字符串」到我的模型