开发者

How to update attributes on database using Ruby on Rails 2.3.9

开发者 https://www.devze.com 2023-03-20 06:18 出处:网络
I\'m working on a controller action and, what I want to do is to modify a record in the data base. I have the following code:

I'm working on a controller action and, what I want to do is to modify a record in the data base.

I have the following code:

def save_reserve
    @pnr = P开发者_Go百科nr.find_by_email(params[:pnr][:email])
    if (!@pnr.blank?)
      @pnr.update_attributes(params[:pnr])
    else
      @pnr = Pnr.new(params[:pnr])
      if @pnr.save
        ...
      else
        ...
      end
    end
  end

Why @pnr.update_attributes(params[:pnr]) doesn't work?

However, if I do:

      @pnr.update_attribute(:name, params[:pnr][:name])
      @pnr.update_attribute(:lastname, params[:pnr][:lastname])
      @pnr.update_attribute(:phone, params[:pnr][:phone])
      @pnr.update_attribute(:addr, params[:pnr][:addr])

it works... Am I missing something?

Thank you!


model.update_attributes(hsh) is effectively the same as calling model.attributes = hsh; model.save - it's subject to any callbacks and validations on the object.

model.update_attribute(field, value) directly updates that field in the database without any callbacks or validation being run.

If you check the return value of @pnr.update_attributes(params[:pnr]) you'll probably see it's false. You should have an if [...] check around this in the same way as you do for your save call in the new branch of your cose

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号