Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to perform bulk update in rails?

Srinivasa Varma D 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

Reply
Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.