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

  • Home
  • SEARCH
  • 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 8110483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:46:16+00:00 2026-06-06T01:46:16+00:00

I am doing Rails 3.2.3 and have an issue: I have 2 models Product

  • 0

I am doing Rails 3.2.3 and have an issue:

I have 2 models Product and User already. The User table was created when I setup devise. I want to make product belong to user and here is what I did:

rails g migration AddUserToProduct user_id:integer

In product model, I add:

belongs_to :user

In User model, I add:

has_many :products

I ran

rake db:migrate 

and i looks fine. To test that, I go to the web interface, log in and make a new product then using Rails console, I type in

Product.last

I am confused since user_id:nil which means there’s no reference between the user and the product.

Is there anything wrong with my code ? What is the proper way to create a reference column between two existing Models ?

Thanks for your helps

  • 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-06-06T01:46:27+00:00Added an answer on June 6, 2026 at 1:46 am

    Could you show your Products controller? Most likely, you have something like

    def create
      @product = Product.new(params[:product])
      ...
    end
    

    if so, change it to

    def create
      @product = current_user.products.build(params[:product])
      ...
    end    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a Rails-version of a website where I have already created
I created a rails model by doing script/generate model Customer name:string address:string city:string state:string
What I'm basically doing is sharing some models across multiple projects. I have a
I want to have a text box that the user can type in that
I'm having some issues with routing in Rails. I already have a model in
I'm pretty new to Rails and have an issue which I can't quite get
We have already built a rails app that has several users and an image
I have a Rails app with some basic models. The website displays data retrieved
At my new job I am doing rails development and I am on Ubuntu
Im using Emacs with Ruby Electric and the Rinari extension for doing Rails development

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.