使用self来引用rails模型中的当前实例有哪些规则

时间:2016-03-22 19:11:40

标签: ruby-on-rails ruby activerecord

我在rails应用程序的一些复杂部分中遇到了一个错误,当你需要使用self来引用Rails模型中的当前对象时,我很好奇。我有(下面有问题的行并注释掉):

class Item < ActiveRecord::Base
....
def item_instore_enabled_changed list_is_instore_enabled # would be true or false
  if item_is_instore_enabled
    Rails.logger.info("this will be true with is_enabled_item: #{is_enabled_item}")
    self.is_enabled=true if self.is_enabled_item
    # is_enabled=true if is_enabled_menu_item - this didn't work
  end
  Rails.logger.info("BEFORE item save and is_enabled: #{is_enabled}")
  self.save! 
  # save  - didn't work
  Rails.logger.info("AFTER item save and is_enabled: #{is_enabled}")
end

基本上,我必须指定self.is_enabled而不只是is_enabled来指定一个值,并且只能调用save!并且必须调用self.save!

在对象中引用self的规则是什么,或者是否有其他错误导致上述代码出现问题?

0 个答案:

没有答案