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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:04:28+00:00 2026-05-13T11:04:28+00:00

I have a user stored in a session which I would like to add

  • 0

I have a user stored in a session which I would like to add as the owner of a comment. Rather than having a hidden field for user_id, I would like to add the user before the comment is saved in the controller.

What would be the best way to do this?

@comment = @post.comments.create(params[:comment])

Thanks.

  • 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-13T11:04:28+00:00Added an answer on May 13, 2026 at 11:04 am

    There are a few strategies that work fairly well. You can have a call in the controller that staples the User on to the created comment:

    def create
      @comment = @post.comments.build(params[:comment])
      @comment.user = session_user
      @comment.save!
    
      redirect_to(post_path(@post))
    
    rescue ActiveRecord::RecordInvalid
      # Take appropriate action, such as show comment create form
      render(:action => 'new')
    end
    

    Another way is to use something like model_helper (http://github.com/theworkinggroup/model_helper/) to provide access to controller properties within the model environment:

    class ApplicationController < ActionController::Base
      # Makes the session_user method callable from the ActiveRecord context.
      model_helper :session_user
    end
    
    class Comment < ActiveRecord::Base
      before_validation :assign_session_user
    
    protected
      def assign_session_user
        if (self.user.blank?)
          self.user = session_user
        end
      end
    end
    

    This method is more automatic, but at the price of transparency and possibly complicating your unit test environment.

    A third approach is to merge in the parameters on the create call:

    @comment = @post.comments.build((params[:comment] || { }).merge(:user => session_user))
    

    This has the disadvantage of not working very well if some of the properties of your model are protected, as they probably should be in any production environment.

    Another trick is to create a class method that helps build things for you:

    class Comment < ActiveRecord::Base
      def self.create_for_user(user, params)
        created = new(params)
        created.user = user
        created.save
        created
      end
    end
    

    This is called on the relationship and will build in the correct scope:

    @comment = @post.comments.create_for_user(session_user, params[:comment])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.