在 Rails 4.0.4 中,这段代码有效:
mailboxes = Mailbox.order(:mailbox)
mailboxes.keep_if do |mailbox|
# test
end
在 Rails 4.1.0 中它打破了 NoMethodError (undefined method keep_if for <Mailbox::ActiveRecord_Relation:0x5494f80>)
并且必须更改为
mailboxes = Mailbox.order(:mailbox).to_a
mailboxes.keep_if do |mailbox|
# test
end
没有找到相关信息
有什么想法吗?
最佳答案
它在 release notes 中对于 rails 4.1
Relation no longer has mutator methods like #map! and #delete_if. Convert to an Array by calling #to_a before using these methods. (Pull Request)
因为 keep_if
是一个增变器方法,所以它从 Relation
中移除
关于ruby - Rails 4.1 ActiveRecord::relation 不再像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973737/