Dr.Gonzo
Dr.Gonzo

Reputation: 107

Update information in a Hash associated to an ActiveRecord rails 5

I have an two hashes where one is called current_savings and one called mambu_repayments

current_savings.select {|s| s[:status] == 'repaid' }

In rails c the above will output any savings where status is repaid.

mambu_repayments.last

In rails c the above line will output the repayment. This hash has key value element "state"=>"PAID"

I want change that state value to COMPLETE for every last repayment where current_savings status is marked repaid.

In my relevant controller I have tried the following:

 # check saving is not nil and status is :repaid.
    # Retrieve last repayment from @repayments and check if it has status PAID

    if current_savings != nil? && current_savings.status == 'repaid'
      mambu_repayments = current_savings.get_repayments_with_parents.select{|s| s["state"] == 'PAID'}
      mambu_repayments.last = 'COMPLETE'
    end

Upvotes: 0

Views: 40

Answers (1)

Nimish Gupta
Nimish Gupta

Reputation: 3175

Please look at the below snippet

# check saving is not nil and status is :repaid.

# Retrieve last repayment from @repayments and check if it has status PAID

if current_savings != nil? && current_savings.status == 'repaid'
  mambu_repayments = current_savings.get_repayments_with_parents.select{|s| s["state"] == 'PAID'}
  # If you dont' want to run callbacks and validations
  mambu_repayments.last.update_columns(state: 'COMPLETE') 
  # If you want to run callbacks and validations
  # mambu_repayments.last.update(state: 'COMPLETE') 
end

Upvotes: 1

Related Questions