Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails &引用;没有这样的专栏“;模型类型联接作用域出错_Ruby On Rails_Ruby On Rails 3_Join_Scope_Associations - Fatal编程技术网

Ruby on rails &引用;没有这样的专栏“;模型类型联接作用域出错

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和Customer),它们在DB中共享一个表,使用Rails的“type”参数将它们分开:

# 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