Peege151
Peege151

Reputation: 1560

Saving to DB from Model.rb

When an new order_preview is created, I call USPS for shipping options. If a user updates their zip, I would like the ship_option to reset

Edit: I am no longer calling the intial API call from the view, rather I do an after_create method in the controller.

def get_ship_options
ship_options = {}
      @order_preview.fedex_rates.each do |k, v|
          if k.service_name == "FedEx Ground Home Delivery" || k.service_name == "FedEx 2 Day" || k.service_name == "FedEx Standard Overnight"
      ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
    end
      end
      @order_preview.usps_rates.each do |k, v|
          if k.service_name == "USPS Priority Mail 1-Day"
      ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
          end
      end
  @order_preview.ship_option_hash = ship_options.map { |k,v| ["#{k} - #{v}","#{k} - #{v}" ] }
  @order_preview.save
end

I tried using the answers you guys provided, but the before_save didn't actually save the shiphash the way @order_preview.save does at the end of the above method.

I tried using the same idea, but zip_changed? doesn't work in the controller.

How can I save the new hash that is pulled from the model directly over to the @order_preview ?

From the model I now have

Model.rb

def clear_hash
 if zip_changed? 
  get_shipping_rates
 end
end

and

       ship_options = {}
        fedex_rates.each do |k, v|
            if k.service_name == "FedEx Ground Home Delivery" || k.service_name == "FedEx 2 Day" || k.service_name == "FedEx Standard Overnight"
                ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
            end
        end
        usps_rates.each do |k, v|
            if k.service_name == "USPS Priority Mail 1-Day"
                ship_options["#{k.service_name}"] = "#{number_to_currency(k.price.to_f / 100)}"
            end
        end
        ship_option_hash = ship_options.map { |k,v| ["#{k} - #{v}","#{k} - #{v}" ] }
**save ship_option_hash to @order_preview.ship_option_hash**

Upvotes: 0

Views: 43

Answers (2)

Marlin Pierce
Marlin Pierce

Reputation: 10081

class OrderPreview
  before_save :check_state

  def check_state
    if zip_changed?
      ship_option_hash = nil
    end
  end
  ...
end

class OrderPreviewController
  def update
    @order_preview.update(order_preview_params)
  end
...
end

Upvotes: 1

Farrukh Abdulkadyrov
Farrukh Abdulkadyrov

Reputation: 717

Try changing your callback from after_save to before_save. Record considered changed until the changes are not persisted. Changes are lost when you save your object, that's why your record is unchanged when you check for changes in after_save callback.

It should work this way:

before_save :clear_hash, if: :zip_changed?

def clear_hash
   ship_option_hash = nil
end

This way the changes will be saved, because you use before_save. In your code, changes were not saved, because you used after_save callback

You controller:

def update
 respond_to do |format|
  if @order_preview.update(order_preview_params)
    flash[:notice] = "Record was successfully updated"
  else
    flash[:alert] = "Record  was not updated"
  end
 end
end

Upvotes: 0

Related Questions