Ruby Rails 4.1 ActiveRecord::关系不再像数组
在Rails 4.0.4中,此代码起作用:Ruby Rails 4.1 ActiveRecord::关系不再像数组,ruby,activerecord,ruby-on-rails-4,Ruby,Activerecord,Ruby On Rails 4,在Rails 4.0.4中,此代码起作用: mailboxes = Mailbox.order(:mailbox) mailboxes.keep_if do |mailbox| # test end 在Rails 4.1.0中,它与NoMethodError(未定义的方法keep_if for)断开 必须改成 mailboxes = Mailbox.order(:mailbox).to_a mailboxes.keep_if do |mailbox| # test end 我找不到任
mailboxes = Mailbox.order(:mailbox)
mailboxes.keep_if do |mailbox|
# test
end
在Rails 4.1.0中,它与NoMethodError(未定义的方法keep_if for)断开
必须改成
mailboxes = Mailbox.order(:mailbox).to_a
mailboxes.keep_if do |mailbox|
# test
end
我找不到任何有关这方面的信息
有什么想法吗?rails 4.1已经有了
关系不再有像#map这样的变异方法!如果有,请删除。
在使用这些方法之前,通过调用#to _a将其转换为数组。(拉
请求)
由于keep\u if
是一个mutator方法,它将从关系中删除
,这就解释了为什么我无法在“respond\u with User.all”上获得正确的JSON响应。但是对User.all.to_执行respond_时,正确地调用了我的to_json方法,并给出了所需的结果。谢谢!在调用Modal.order
后解决了我的错误:NoMethodError(未定义的方法“sort\u by!”)