【问题标题】:Toggle! Bang Change in Rails 4切换! Rails 4 中的巨大变化
【发布时间】:2016-03-02 18:57:54
【问题描述】:

为了记录的目的试图解开一个谜。 我们正在从 Rails 3.2 升级到 Rails 4。

在 Rails 3.2 代码中,我们有(嗯,有):

def update_object_special_eligibility
  object.toggle!(:can_run_on_special) if object && object.can_run_on_special? && ineligible_for_special?
end

我意识到这并没有为您提供所需的所有信息,但这里是错误的。您应该知道can_run_on_special 是位域而不是官方属性,尽管我认为它实际上并不相关。只是想我会让你知道我们为什么会收到错误:

1) Error:
ObjectStatusTest#test_should_mark_object_as_not_eligible_to_run_on_special_for_specific_unschedule_reasons:

ActiveModel::MissingAttributeError: can't write unknown attribute     `can_run_on_special'
    config/initializers/acts_as_audited.rb:280:in `write_attribute_with_audit'
    app/models/object_status.rb:437:in `update_deal_amazon_eligibility'
    test/unit/object_status_test.rb:481:in `block in <class:ObjectStatusTest>'
    test/fast_test_helper.rb:99:in `call'
    test/fast_test_helper.rb:99:in `block in <class:TestCase>'

我们将其更改为适用于 Rails 4 的代码:

def update_object_special_eligibility
  if object && object.can_run_on_special? && ineligible_for_special?
  object.update_attributes! :can_run_on_special => false
end

我的怀疑是,因为toggle! 在绕过验证方面的操作类似于update_attribute,所以它只是返回false 来保存属性,因此,toggle! 不再起作用,因为更新了属性和保存不起作用。但我不确定情况是否如此。我希望这里有人可能知道为什么这个 ActiveRecord::Persistence 方法 (toggle!) 在 Rails 4 中的操作方式可能不同。或者,是“属性”的更新和保存可能操作方式不同?

非常感谢!

【问题讨论】:

    标签: ruby-on-rails-4 toggle persistence rails-activerecord


    【解决方案1】:

    看起来acts_as_audited 正在参与其中,并且不是位域/切换精通,这是我的猜测。太多的厨师在这里入侵他们的商品。 (位域和acts_as_audited)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多