【发布时间】:2010-06-15 14:01:33
【问题描述】:
我正在将应用程序更新到 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
观点:
<%- @products.each do |p| -%>
<%= p.created_at %><br />
<%- end -%>
我在 Rails 日志中收到此错误:
Mysql::Error: Unknown column 'products.user_id' in 'where clause': SELECT `products`.* FROM `products` WHERE (`products`.user_id = 1)
它应该会看到belongs_to :owner 并查找名为owner_id 的外键。我什至尝试显式设置外键,但这不起作用。我还检查了灯塔是否存在可能的 Rails 3 错误,但没有运气。
【问题讨论】:
标签: ruby-on-rails activerecord belongs-to