0

在我的模型中,我有:

  after_initialize :populate_neutral_post_option
  before_create :make_neutral_option_last

  module RequiredOptions
    NEUTRAL = "Neutral"
  end

  def populate_neutral_post_option
    if self.new_record? && post_options.map(&:text).exclude?(RequiredOptions::NEUTRAL)
      post_options.build(text: RequiredOptions::NEUTRAL)
    end
  end

  def make_neutral_option_last
    post_options.rotate!(1)
  end

我得到一个 NoMethodError 说.rotate!是未定义的。没有爆炸的普通旧版.rotate工作正常,但我希望将结果保存在原始数组中。

目标是让这个“中性”帖子选项出现在列表的最后。我可以.rotate在视图中调用数组,但这似乎是不必要的重复工作,我可能应该只更改原始数组,对吧?有什么建议吗?

运行红宝石 2.7.0p0

4

0 回答 0