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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:51:29+00:00 2026-06-18T20:51:29+00:00

I currently have the following controller method: def update_all params[:users].each do |product, user| @product

  • 0

I currently have the following controller method:

def update_all

    params[:users].each do |product, user|
       @product = Product.find(product)
       @product.update_attribute(:user_id, user)
    end
    redirect_to :back, :flash => { :notice => "Updated users" }
  end

This works as expected, however the problem is that the params are being passed via a select_tag, which has a default value — so if just one select tag is changed and the form is submitted, all the other records will be updated to the default value. How can I check if the attribute has change from its previous value, and only if it has changed to update it?

  • 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-18T20:51:31+00:00Added an answer on June 18, 2026 at 8:51 pm

    you can check for dirty attributes and presence

    params[:users].each do |product, user_id|
      @product = Product.find(product)
    
      if user_id != '--' && user_id != @product.user_id
        @product.update_attribute :user_id, user
      end
    
      # or
      # if user_id != '--'
      #   @product.user_id = user_id
      #   @product.save if @product.user_id_changed?
      # end
    end
    

    or set the default values in your select tag (from Sending extra param value via select_tag)

    <%= collection_select :user, :id, User.all, :id, :name, { selected: product.user_id }, { name: "users[#{product.id}]" } %>
    

    so you don’t have to worry about default values.

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

Sidebar

Related Questions

I currently have the following controller method in a Rails app: def index @entries
In my Schedules Controller, I have the following method: def create @schedule = current_user.team.create_schedule(params[:schedule])
In application_controller I have the following method: def authorized_for_roles(*roles) roles.each{|role_name| return true if current_user.has_role?(role_name)}
I have an ArticleController with the following code : def edit @article = current_user.articles.find(params[:id])
I have the following method in a controller: # GET /units/1 def show @unit
I have the following application_controller method: def current_account @current_account ||= Account.find_by_subdomain(request.subdomain) end Should I
I have the following method on one of my models: def remaining_stock if initial_stock
In my controller, i have a method defined as: def self.store_pdf(id) ... end in
I have following method in one of my module def current_user end def current_user=(new_user)
Currently I have following XML: <Rowsets> <Rowset> <Row> <DrilldownDepth>0</DrilldownDepth> <ScheduleWeek>---</ScheduleWeek> <ABC>---</ABC> <PQR>1500</PQR> <XYZ>15000</XYZ> <Quant>285</Quant>

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.