Ruby on rails &引用;没有这样的专栏“;模型类型联接作用域出错
我有两个模型(Person和Customer),它们在DB中共享一个表,使用Rails的“type”参数将它们分开:Ruby on rails &引用;没有这样的专栏“;模型类型联接作用域出错,ruby-on-rails,ruby-on-rails-3,join,scope,associations,Ruby On Rails,Ruby On Rails 3,Join,Scope,Associations,我有两个模型(Person和Customer),它们在DB中共享一个表,使用Rails的“type”参数将它们分开: # Person.rb class Person < ActiveRecord::Base ... end # Customer.rb class Customer < Person has_many :orders end 但我收到了以下错误: ActiveRecord::StatementInvalid: SQLite3::SQLExceptio
# Person.rb
class Person < ActiveRecord::Base
...
end
# Customer.rb
class Customer < Person
has_many :orders
end
但我收到了以下错误:
ActiveRecord::StatementInvalid:
SQLite3::SQLException: no such column: orders.customer_id: SELECT "people".* FROM "people" INNER JOIN "orders" ON "orders"."customer_id" = "people"."id" WHERE "people"."type" IN ('Customer') AND (orders.created_at > '2013-06-18 16:47:44.726372')
联接正在查找“orders.customer\u id”,而它应该查找“orders.person\u id”。如何进行此更正?您需要指定外键:
class Customer < Person
has_many :orders, foreign_key: person_id
end
class客户
您需要指定外键:
class Customer < Person
has_many :orders, foreign_key: person_id
end
class客户
啊,我明白了。我尝试过将外键
属性添加到订单
模型中。显然,我需要将其添加到客户。谢谢你的快速帮助!啊,我明白了。我尝试过将外键
属性添加到订单
模型中。显然,我需要将其添加到客户。谢谢你的快速帮助!
class Customer < Person
has_many :orders, foreign_key: person_id
end