Skip to main content

How to perform bulk update in rails?

Databases • Asked by Srinivasa Varma

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



Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.