我有模型order.rb
class Order < ActiveRecord::Base
belongs_to :customer
has_many :products, dependent: :destroy, class_name: 'OrderProduct'
has_many :attributes, dependent: :destroy, class_name: 'OrderAttribute'
has_one :invoice_address, class_name: 'OrderAddress'
has_one :delivery_address, class_name: 'OrderAddress'
validates :number, presence: true
validates :total_amount, presence: true
end
当我运行rspec时,我收到错误
undefined method `keys' for #<ActiveRecord::Associations::CollectionProxy []>
保存订单时在线。当我发表评论时
has_many :attributes, dependent: :destroy, class_name: 'OrderAttribute'
它按预期工作。我的rspec文件是:
require "spec_helper"
describe Order do
before { @order = Order.new(:number => '132', :total_amount => 123, :paid_amount => 123) }
it { assert_equal true, @order.valid? }
describe "number cannot be empty" do
before { @order.number = nil }
it { assert_equal false, @order.valid? }
end
describe "total_amount cannot be nil" do
before { @order.total_amount = nil }
it { assert_equal false, @order.valid? }
end
describe "can add products" do
before do
@order.save
@order.products.create(:name => 'test', :price => 123, :qty => 1)
end
it { assert_equal 1, @order.products.count }
end
# describe "can add attribute" do
# before do
# @order.save
# @order.attributes.create(:key => 'some_key', :value => '12')
# end
# it { assert_equal 1, @order.attributes.count }
# it { assert_equal 'some_key', @order.attributes.first.key }
# it { assert_equal '12', @order.attributes.first.value }
# end
end
我注释掉了订单属性的测试,因为它产生了另一个错误。怎么解决? 我有
的order_comment.rb文件class OrderAttribute < ActiveRecord::Base
belongs_to :order
end
它有什么问题?
答案 0 :(得分:3)
将attributes
重命名为导致问题的原因
如果你有一个has_many被叫属性,你就无法访问你的 对象属性了;只有关联的对象Original Rails 维基 - 附加词语
相关链接: