如何从模型内部访问属性

How to access attributes from inside a model

我有一个名为 item 的模型,其中有一列名为 price。我假设 price 将作为实例变量存储在 item 中,但是方法

def price
  @price
end

Returns 没什么。所以我的问题是如何从我的模型内部访问和覆盖从数据库中提取的价值价格。

你按照Rails guides

使用
def price
  self[:price]
end