如何更新自我模型轨道属性 [英] how to update attributes in self model rails

查看:171
本文介绍了如何更新自我模型轨道属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新自身模型轨道?

how to update self model in rails?

class BillSubTotal < ActiveRecord::Base
  def self.total
    self.update_attributes(:total => 150)
  end
end

我想从自我模型更新模型BillSubTotal价值?

i want update value in model BillSubTotal from self model?

推荐答案

取决于你想要做的你如何去这个问题。

Depends on what you're wanting to do as how you'd go about this.

导轨自动创建的getter / setter方法​​的数据库列,所以你不必明确创建功能,并且可以只设置方法如下所示:

Rails automatically creates the getter/setter methods for database columns, so you don't have to create that functionality explicitly, and can just set the method like so:

class BillSubTotal < ActiveRecord::Base
  def update_total
    self.total = 150
  end
end

或者,如果你正在使用的类的实例直接:

Or if you're working with an instance of the class directly:

bill_subtotal_instance.total = 150

这篇关于如何更新自我模型轨道属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆