在我的模型中,我有:
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