3

次のコードがあります。

  def incoming_acceptation(incoming_code)
    if invite_code == incoming_code
      accepted = true
      self.save
      true
    else
      false
    end
  end

しかし、true に変更して保存することはなく、以前の状態である false のままです。

@i.incoming_acceptation(incoming_code) => true
@i.accepted => false
4

2 に答える 2

6

私はお勧め:

def incoming_acceptation(incoming_code)
  update_attribute(:accepted, true) if invite_code == incoming_code
end

update_attributeその属性を変更して保存します。一度に複数の属性を変更するために Hash を受け入れるupdate_attributes( に注意してください)もあります。s

@obj.update_attributes(:accepted => true, :accepted_at => Time.now)

注:例のように、変更と保存が成功するupdate_attributeupdate_attributes両方とも戻ります。true

于 2010-10-27T11:13:04.483 に答える
3
self.accepted = true
于 2010-10-27T10:35:09.517 に答える