0

I have a Bank model with attributes id, name, code and rate. I just changed the rate method of bank model. But the problem is when I am updating the bank objects, it considers the condition in the rate attribute method. I want to update the original value whatever I am getting from the inputs.

Ref attribute of Bank model

  def rate
    authorised? ? (super * 100) : 0
  end

Update method in banks controller

  def update_banks
    banks = []
    banks = get_all_banks(params[:banks])

    begin
      ActiveRecord::Base.transaction do
        if banks.each { |b| b.save! }
          redirect_to banks_url, notice: 'Banks were successfully updated.'
        else
          raise ActiveRecord::Rollback
        end
      end
    rescue Exception=>e
      redirect_to edit_rates_rates_path(bank_id: params.fetch(:select_bank)[:bank_id], product_id: params.fetch(:select_product)[:product_id], date: params[:date])
      flash[:error] = ["<b>Rates could not be updated at the moment.</b>"]
      rates.each { |rate| rate.errors.full_messages.each { |message| flash[:error] << message } }
    end 
 end

I am getting list of bank objects. Is there any way to tell rails to ignore "rate" method of bank model and consider the input value?

Thanks

Larrikin
  • 155
  • 1
  • 12
  • let's think about it from other side, when do you want rails to consider rate method? – Md. Farhan Memon Jun 15 '17 at 07:46
  • 1
    Why are you doing an `authorised?` check **in the model**?? How does this even work? The model has no concept of "who is the `current_user`". – Tom Lord Jun 15 '17 at 09:09
  • 1
    Also, [don't rescue from `Exception`](https://stackoverflow.com/q/10048173/1954610). – Tom Lord Jun 15 '17 at 09:11
  • Also, why are you setting `banks = []`? That line of code is doing literally nothing. – Tom Lord Jun 15 '17 at 09:15
  • Also, is your `update` action actually *updating* anything?? It looks like you're just re-saving the existing data! – Tom Lord Jun 15 '17 at 09:16
  • 1
    Also, why are you putting `` in your flash message? Use a CSS class instead. You must be calling `html_safe` on the message, which probably leaves you open to XSS attacks. – Tom Lord Jun 15 '17 at 09:17

0 Answers0