Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails中模型属性的延迟设置?_Ruby On Rails_Model - Fatal编程技术网

Ruby on rails Rails中模型属性的延迟设置?

Ruby on rails Rails中模型属性的延迟设置?,ruby-on-rails,model,Ruby On Rails,Model,我有一个通过解析数据文件生成的模型。有几个有趣的值没有出现在原始数据文件中,但可以从这些值中派生出来 然而,这些派生值中有许多计算起来很昂贵,所以我希望在计算完之后将它们存储在数据库中 我曾尝试这样做: def profit if not self[:profit] update_attribute :profit, expensive_computation end self[:profit] end 不幸的是,这只完成了一半的工作。计算出的值会被存储,但每次我调用pro

我有一个通过解析数据文件生成的模型。有几个有趣的值没有出现在原始数据文件中,但可以从这些值中派生出来

然而,这些派生值中有许多计算起来很昂贵,所以我希望在计算完之后将它们存储在数据库中

我曾尝试这样做:

def profit
  if not self[:profit]
    update_attribute :profit, expensive_computation
  end
  self[:profit]
end
不幸的是,这只完成了一半的工作。计算出的值会被存储,但每次我调用profit时,它都会再次计算该值

更新:

原来问题不在方法中,而是在前面的find_by_sql语句中,该语句使Rails对模型的id感到困惑。我最终得到的代码是:

def profit
  unless read_attribute(:profit)
    update_attribute(:profit, expensive_computation ) 
    self.save!
  end
  read_attribute(:profit)
end

创建一个新方法并从中删除读取逻辑。也许是这样的

 def update_profit
    update_attribute :profit, expensive_computation
 end
这样,您就可以分离阅读逻辑,只需使用rails提供的
利润

 @model.profit

其中@model是属性
profit
所在的任何类的实例。

创建一个新方法并从中删除读取逻辑。也许是这样的

 def update_profit
    update_attribute :profit, expensive_computation
 end
这样,您就可以分离阅读逻辑,只需使用rails提供的
利润

 @model.profit

其中@model是属性
profice
所属的任何类别的实例。

代码应该放在哪里取决于何时可以计算利润(例如,记录第一次保存时,或者以后会保存时),以及利润是否会发生变化

一般来说,最好将计算放在某种类型的
中,然后再\u create
/
再\u save
调用,该调用仅在可以计算和/或已更改时保存它

vrish88是正确的,您可能应该将
利润
方法放在一边,除非您确实需要在需要数据时动态计算利润。如果这是您真正需要的,请尝试:

def profit
  update_attribute(:profit, expensive_computation) unless read_attribute(:profit)

  read_attribute(:profit)
end

代码应该放在哪里将取决于何时可以计算利润(例如,记录第一次保存时,或者有时会保存到以后?),以及利润是否会发生变化

一般来说,最好将计算放在某种类型的
中,然后再\u create
/
再\u save
调用,该调用仅在可以计算和/或已更改时保存它

vrish88是正确的,您可能应该将
利润
方法放在一边,除非您确实需要在需要数据时动态计算利润。如果这是您真正需要的,请尝试:

def profit
  update_attribute(:profit, expensive_computation) unless read_attribute(:profit)

  read_attribute(:profit)
end

这对我来说很好。我的第一个想法是:如果不是self[:profit]
行,您是否在
中有输入错误?

这对我来说很好。我的第一个想法是:如果不是self[:profit]
行,您是否在
中有输入错误

def profit
  super || update_attribute(:profit, expensive_computation)
  return super
end
如果profit列中包含信息,只需调用对象的原始profit方法,该方法将从数据库返回
profit
列的内容

否则,(即
profit
列的值为
nil
)使用
update\u属性
方法计算
profit
值并将其存储到数据库,然后再次从数据库返回值

如果您希望获取
profit
的值(如果它已经在数据库中),或者如果昂贵的计算已经运行,则只需执行此操作(
update\u attribute
返回true或false):

如果profit列中包含信息,只需调用对象的原始profit方法,该方法将从数据库返回
profit
列的内容

否则,(即
profit
列的值为
nil
)使用
update\u属性
方法计算
profit
值并将其存储到数据库,然后再次从数据库返回值

如果您希望获取
profit
的值(如果它已经在数据库中),或者如果昂贵的计算已经运行,则只需执行此操作(
update\u attribute
返回true或false):