【发布时间】:2013-05-26 15:20:24
【问题描述】:
所以,我正在尝试使用以下测试来测试我的模型关联:
it 'retrieve items registered under person' do
p = FactoryGirl.create(:person)
o = FactoryGirl.create(:order, customer: p)
o.customer.should == p
i = FactoryGirl.create(:item)
o.items << i
o.save
p.items.count.should == 1
end
我的模型:
class Person < AR:Base
has_many :orders, :as => :customer
has_many :items, :through => :orders
end
class Order < AR:Base
has_many :items
belongs_to :customer, :class_name => Person
end
class Item < AR:Base
belongs_to :order
has_one :customer, :class_name => Person, :through => :order
end
但是当我运行测试时,它给了我以下错误:
SQLite3::SQLException: no such column: orders.customer_type: SELECT COUNT(*) FROM "items" INNER JOIN "orders" ON "items"."order_id" = "orders"."id" WHERE "orders"."customer_id" = 1 AND "orders"."customer_type" = 'Person'
我做错了什么?
更新: 问题出在 ':as => :customer' 位上。但我真正的问题是测试。我应该在创建项目时分配订单。
【问题讨论】:
-
订单表中可能没有customer_type列。
-
是的,我知道了。但它为什么要寻找列 customer_type?
-
你确定这些模型不使用单表继承吗?因为这会导致 ActiveRecord 查找
model_type。 -
你能发布你的 db/schema.rb 吗?
标签: ruby-on-rails ruby-on-rails-3 rspec sqlite rspec-rails