我有一个 id 数组,我想使用活动记录查询从数据库中查找它们各自的记录。例如:ids = [2, 3, 1]
现在,对于我来说,要查找 id 是数组中的其中之一的特定模型的所有记录,在较低版本的 Rails 中,我想我可以执行以下操作:
Model.find_all_by_id([2, 3, 1])
但根据这个帖子 https://github.com/rails/rails/issues/17357,
These methods were deprecated on 4.0 and removed at 4.1. If you want to
keep sing then you need to include this gem
https://github.com/rails/activerecord-deprecated_finders
我的问题是,在rails4.2 中,我是否可以在不必包含 gem 的情况下做到这一点?
Thanks.
这应该有效:
array_of_ids = [2,3,1]
Model.where(id: array_of_ids)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)