ruby-on-rails – 为什么我在Rails 4中获得连接表的未知主键例外?

这些是我的模特:
class Product
  has_many :line_items
  has_many :orders,:through => :line_items
end

class LineItem 
  belongs_to :order
  belongs_to :product
end

class Order
    has_many :line_items
    has_many :products,:through => :line_items
end

来自schema.rb:

create_table "line_items",id: false,force: true do |t|
    t.integer  "order_id"
    t.integer  "product_id"
    t.integer  "quantity"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

我刚刚升级到Rails 4,我的连接表停止工作.如果我执行@ order.line_items,它会抛出异常“模型LineItem中的表line_items的未知主键”. @ order.products按预期工作.

我已经尝试删除并重新创建line_items表,我已经尝试安装protected_attributes gem,但没有任何改变.

这是the trace.

解决方法

在模型添加
self.primary_key = [:order_id,:product_id]

我认为确保这些列上有索引是明智的.您可以使用以下迁移创建一个

add_index :line_items,[:order_id,:product_id]

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...