All threads / How to perform bulk update in rails?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to perform bulk update in rails?

Srinivasa Varma asked in Databases

Hi there,

Here is the problem in more detail. I tried to perform bulk update on rails model, but ended up with normal save method. I have some number of records and i wanted to update their values with respect to performed calculations. But i could not find a solution to do that. Any ideas appereciated.

Below is my rails task code:

  wsp = Wsp.includes(:collections)
    wsp.each do |ws|
         ws.set_margin_attributes
         ws.save(validate: false)
    end

and Wsp model contains set_margin_attributes method like below

    def set_margin_attributes(ws_calculations)
            self.attributes = {
                total_amount: ws_calculations[:total_amount],
                margin: ws_calculations[:margin],
                margin_percent: ws_calculations[:margin_percent],
                inr_margin: ws_calculations[:inr_margin]
            }
        end

Can some one suggest is there any otherway to do this.

Thanks in advance

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,629+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.