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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:01:08+00:00 2026-06-18T08:01:08+00:00

Rails 3.2. I am using the following code to associate user_id to the record:

  • 0

Rails 3.2. I am using the following code to associate user_id to the record:

  # review.rb
  class Review < ActiveRecord::Base
    belongs_to :reviewable, :polymorphic => true, :counter_cache => true
  end

  # reviews_controller.rb
  def create
    @review = @reviewable.reviews.new(params[:review])
    @review.user_id = current_user.id
    if @review.save
      flash[:success] = 'Thanks for adding your review.'
      redirect_to @reviewable
    else
      flash[:error] = 'Error adding review,  please try again.'
      redirect_to @reviewable
    end
  end

I want to find a way to use this, but it keeps saying that the current_user is not defined, but I could find the current_user object:

  def create
    @review = @reviewable.current_user.reviews.create(params[:review]
    if @review.save
      flash[:success] = 'Thanks for adding your review.'
      redirect_to @reviewable
    else
      flash[:error] = 'Error adding review,  please try again.'
      redirect_to @reviewable
    end
  end
  • 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-18T08:01:10+00:00Added an answer on June 18, 2026 at 8:01 am

    If you can post your code to what the @reviewable object is, it might help to give a more specific answer. But if you want a one liner, you can do something like this:

    @review = @reviewable.reviews.build(params[:review].merge({:user_id => current_user.id})
    

    But personally, i think your original looks better as it’s easier to read.

    As an additional note, your second example also calls create and save. You don’t need to call both, as create saves the object when accepting a hash of parameters. Save is nice to use if you want to initialize an object, modify it in some way, then save it later.

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

Sidebar

Related Questions

Using Rails 3.2. I have the following code: # photo.rb class Photo < ActiveRecord::Base
Using Rails 3.2. I have the following code: # photo.rb class Photo < ActiveRecord::Base
Using the less-rails gem. Using the following code to mix two colours: @base: #ffdc52;
I'm using the following view code in my Ruby (1.9.3) on Rails (3.1) application
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I am using Rails 3.2. I have following models: Blog Comment User class Blog
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
I noticed that the code in the rails framework is using the following convention
I'm trying to style a rails link using css using the following code: <%=
I'm using Rails 3.1 here, and I've got the following code in my view:

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.