Ruby on rails 属于某个自定义类,但该类名称在Rails 3中未生成正确的外键

Ruby on rails 属于某个自定义类,但该类名称在Rails 3中未生成正确的外键,ruby-on-rails,activerecord,belongs-to,Ruby On Rails,Activerecord,Belongs To,我正在将一个应用程序更新到Rails 3,但在创建自定义外键时遇到问题。我有这样的想法: class Product < ActiveRecord::Base belongs_to :owner, :class_name => 'User' ... end class User < ActiveRecord::Base has_many :products ... end class ProductsController < ApplicationContro

我正在将一个应用程序更新到Rails 3,但在创建自定义外键时遇到问题。我有这样的想法:

class Product < ActiveRecord::Base
  belongs_to :owner, :class_name => 'User'
...
end

class User < ActiveRecord::Base
  has_many :products
...
end

class ProductsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @products = current_user.products
  end
end

它应该看到
属于:owner
,并查找名为
owner\u id
的外键。我甚至尝试过显式地设置外键,但这不起作用。我还检查了lighthouse可能存在的Rails 3错误,但没有发现任何问题。

您需要在
上指定外键,因为它有多个:products
关联,它不会自动知道它反映了
所属的:owner

这应该起作用:

class User < ActiveRecord::Base
  has_many :products, :foreign_key => 'owner_id'
...
end
class用户'owner\u id'
...
结束
从rails文档:

:外键

指定外键 用于协会。默认情况下 这大概就是这个的名字 用小写字母和“\u id”表示的类 后缀。因此,一个人类,使 有许多协会将使用 “person_id”作为默认值 :外键


这是可行的,但我想这有点奇怪,我不必用我的Rails 2应用程序。不确定这是否会被视为一个错误,在Rails 2中应该是相同的:我的表中有两列引用用户表。你知道我会怎么安排推荐人吗?我不能像上面建议的那样只说所有者id,因为我需要所有者和提交者。谢谢
Mysql::Error: Unknown column 'products.user_id' in 'where clause': SELECT     `products`.* FROM       `products` WHERE     (`products`.user_id = 1)
class User < ActiveRecord::Base
  has_many :products, :foreign_key => 'owner_id'
...
end