I am updating the application for Rails 3 and I am having trouble creating a custom foreign key. I have something like this:
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
View:
<%- @products.each do |p| -%> <%= p.created_at %><br /> <%- end -%>
I get this error in the Rails log:
Mysql::Error: Unknown column 'products.user_id' in 'where clause': SELECT `products`.* FROM `products` WHERE (`products`.user_id = 1)
It should see belongs_to :owner and look for a foreign key named owner_id . I even tried to explicitly set the foreign key, and this does not work. I also checked the beacon for a possible Rails 3 error, but no luck.
ruby-on-rails activerecord belongs-to
Tony
source share