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

The Archive Base Latest Questions

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

My controller action: def updating_rating input = params[:recommendation_ratings].values input.each do |mini_params| rating_id = mini_params[:rating_id].to_i

  • 0

My controller action:

def updating_rating
    input = params[:recommendation_ratings].values

    input.each do |mini_params|
      rating_id = mini_params[:rating_id].to_i
      recommendation_id = mini_params[:recommendation_id].to_i
      puts rating_id
      puts recommendation_id
      rr = RecommendationRating.find_or_create_by_rating_id_and_recommendation_id(rating_id, recommendation_id)
      rr.update_attributes(:rating_id => rating_id, :recommendation_id => recommendation_id )
    end
   redirect_to :back, :flash => { :notice => "Rating Submitted." }
  end

I am passing in the params from a form_tag in my view. The code works as expected when creating a new relationship, however if you try to update a value and resubmit the form, it adds a new rating, rather that updating the existing rating.

So for example if you have a product with a rating of 1, and change the rating to 2 and resubmit the form, that product will have ratings of 1 and 2. What do I do to update an existing rating, rather than adding an additional rating to a product.

  • 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-18T02:01:49+00:00Added an answer on June 18, 2026 at 2:01 am

    I’m assuming that rating_id is the actual rating that you want to save. With this assumption in mind,

    rr = RecommendationRating.find_or_create_by_rating_id_and_recommendation_id(rating_id, recommendation_id)
    

    will find first a RecommendationRating which has a rating_id of the new rating value. If it can’t find any it will create one. You can fix this by just finding a RecommendationRating first and build a new one if it’s not existing

    rr   = RecommendationRating.find_by_recommendation_id(recommendation_id)
    rr ||= RecommendationRating.new(recommendation_id: recommendation_id)
    
    rr.rating_id = rating_id
    rr.save
    

    If you have associations, it will look cleaner (i’m assuming a has_one relationship here because we’re only talking about 2 columns)

    reco = Recommendation.find recommendation_id
    rr   = reco.recommendation_rating || reco.build_recommendation_rating
    
    rr.rating_id = rating_id
    rr.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What i have: (Action in Controller) def create @test = Test.new(params[:test]) @test.save devicefiles =
My controller action: def single final_static_matrix = Single.final_static_matrix(average_static_matrix, params[:priorities]) ... end In my lib/single.rb:
I have the following controller action: def create @board = Board.new(params[:board]) respond_to do |format|
This is my kategori controller show action: def show @kategori = Kategori.find(params[:id]) @konkurrancer =
I've added the following to my controller's create action: def create BatchUser.mass_insert_v2(params[:batch_user][:batch_name], params[:batch_user] [:batch_description],
def filters = { forUser(controller:'user', action:'*') { before = { user=springSecurityService.getCurrentUser() log.info(came to filter
I have two actions in my controller : def find @item = Item.find(params[:id]) render
So if your Controller Action returns a Model with pre-populated values, how do you
Assume the following code in a Grails controller: def action = { ClassName o
I have the following code as a controller action: def create render :js =>

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.