6
我正在更新應用程序到Rails 3,我在創建自定義外鍵時遇到問題。我有這樣的事情:belongs_to與自定義class_name不生產正確的外鍵在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的錯誤,但沒有運氣。
這個工作,但我想這是一個有點奇怪,我沒有與我的Rails做2應用程序。不知道它會被認爲是一個錯誤 – Tony 2010-06-15 14:44:24
它應該是相同的Rails 2. :) – elektronaut 2010-06-15 14:51:13
我在我的表上有兩列引用用戶表。你知道我將如何設置參考?我不能像上面建議的那樣說owner_id,因爲我需要Owner和SubmittingUser。謝謝! – skaz 2011-04-17 00:25:03