Schaffung Ich bin mit Rails 3.2.3 mit dem Geld-Schienen Juwel und ich habe ein Produktmodell bekam die folgende hat:Deprecation Warnung für Attribut ‚Währung‘
Mein Modell
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
My Test
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
Deprecation Warnung ich erhalte.
% 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
Wie behebe ich diese Verwarnungswarnung?
aktualisieren
Wenn ich ‚Währung‘ in den Tisch arbeiten beginnt es. Muss ich das trotzdem tun?
Super seltsam. Ich musste keine Spalte "Währung" in meine Datenbank einfügen, da dies gemäß dem Juwel optional ist. Ich bemerkte, dass dies nur in meinem FactoryGirl passierte, wenn ich 'Money' Objekte auf die 'price'-Spalte setzen wollte, aber wenn ich auf' Integer' Cents zu 'price_cents' umschaltete, funktionierte es gut, ohne dass Deprecation Warnungen ausgegeben wurden. –