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 7924743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:54:25+00:00 2026-06-03T17:54:25+00:00

I have a rails app, with two separate DB tables, users and products. A

  • 0

I have a rails app, with two separate DB tables, users and products. A user has_many products, and a product belongs_to a user.

When I create a product, I want it to automatically add the user_id to the user_id database column in the products table. What changes to my mvc do I need to make to ensure that the correct user_id is added when a new product is created?

  • 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-03T17:54:27+00:00Added an answer on June 3, 2026 at 5:54 pm

    Just as a suggestion, you may want to go back and accept the answers to some of your previous questions, which will improve your response rate and increase the likelihood someone will answer your questions in the future.

    There are a few ways to do this, one approach:

    Create current user function

    class ApplicationController < ActionController::Base
      private
      # Finds the User with the ID stored in the session with the key
      # :current_user_id This is a common way to handle user login in
      # a Rails application; logging in sets the session value and
      # logging out removes it.
      def current_user
        @_current_user ||= session[:current_user_id] &&
          User.find_by_id(session[:current_user_id])
      end
    end
    

    http://guides.rubyonrails.org/action_controller_overview.html#session

    Make sure to be cognizant of security concerns. A gem like Devise can also help.

    Add to products controller

    class ProductsController < ApplicationController
      def create
        current_user.products.create! params[:product]   # make sure attr_accessible is setup on products
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple user registration page in my rails app that has two
In my Rails app I have a fairly standard has_many relationship between two entities.
I have two forms in my rails app. They both exist in separate tabs
I have an Invoice model in a Rails app. The Invoice has_many Products and
I have a rails app with two models, visits and users. A visit has
for example a have simple rails app with two models, users and books. now
I have a rails app that I'm going to host on engineyard and want
I have two C extensions for Ruby (for a Rails app). I initialize these
I have a Rails app which allows users to upload Excel files which need
So In my rails app I have two resources (rentals, and reservations) which belong

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.