Rails 4.1 ActiveRecord::relation 不再像 Array [英] Rails 4.1 ActiveRecord::relation is no more like Array

查看:23
本文介绍了Rails 4.1 ActiveRecord::relation 不再像 Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 4.0.4 中,此代码有效:

in Rails 4.0.4 this code work:

mailboxes = Mailbox.order(:mailbox)
mailboxes.keep_if do |mailbox|
  # test
end

在 Rails 4.1.0 中,它与 NoMethodError(未定义方法 keep_if for <Mailbox::ActiveRecord_Relation:0x5494f80>)中断

in Rails 4.1.0 it break with NoMethodError (undefined method keep_if for <Mailbox::ActiveRecord_Relation:0x5494f80>)

并且必须更改为

mailboxes = Mailbox.order(:mailbox).to_a
mailboxes.keep_if do |mailbox|
  # test
end

我没有找到任何相关信息

I don't find any information about that

有什么想法吗?

推荐答案

Its in 发行说明 适用于 Rails 4.1

Its in release notes for rails 4.1

Relation 不再有 #map 之类的 mutator 方法!和#delete_if.在使用这些方法之前通过调用 #to_a 转换为数组.(拉请求)

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 是一个 mutator 方法,它从 Relation

Since keep_if is a mutator method, its removed from Relation

这篇关于Rails 4.1 ActiveRecord::relation 不再像 Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆