Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:17:34+00:00 2026-05-15T10:17:34+00:00

I am updating an application to Rails 3 and I am having trouble creating

  • 0

I am updating an application to 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

The view:

<%- @products.each do |p| -%>
    <%= p.created_at %><br />
<%- end -%>

I get this error in my Rails log:

Mysql::Error: Unknown column 'products.user_id' in 'where clause': SELECT     `products`.* FROM       `products` WHERE     (`products`.user_id = 1)

It should see the belongs_to :owner and look for a foreign key called owner_id. I even tried explicitly setting the foreign key and that does not work. I also checked lighthouse for a possible Rails 3 bug but no luck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:17:35+00:00Added an answer on May 15, 2026 at 10:17 am

    You need to specify the foreign key on the has_many :products association, it does not automagically know that it mirrors the belongs_to :owner.

    This should work:

    class User < ActiveRecord::Base
      has_many :products, :foreign_key => 'owner_id'
    ...
    end

    From the rails docs:

    :foreign_key

    Specify the foreign key
    used for the association. By default
    this is guessed to be the name of this
    class in lower-case and "_id"
    suffixed. So a Person class that makes
    a has_many association will use
    "person_id" as the default
    :foreign_key..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble updating my rails app on heroku. I've gotten my app up
I have a code for updating my application resources to current application version. This
I'm updating application for Mac OS X 10.6 and getting this error. I believe
I'm working on creating a self updating application and one issue I'm running into
I'm updating a WinForms application that uses a BackgroundWorker to do something useful when
I am creating a simple application in JSP- JDBC for inserting and updating details
My Rails 3 application has a Product resource. In my ProductsController specs, the generated
I have this deploy.rb file: set :application, UDA set :domain, 10.0.0.59 set :deploy_to, /home/gitek/www/uda/
Is it possible to have a self-updating iPhone application? I believe the answer is
I have a question regarding displaying and updating many elements from a Rails model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.