Help us understand the problem. What is going on with this article?

ActiveRecord findメソッドにidを配列で渡した結果はRelationではない

More than 5 years have passed since last update.

Rails4でfindにidの配列を渡すと、

profiles = Profile.find [1,2,3,4,5]
  # =>  SELECT "profiles".* FROM "profiles"  WHERE "profiles"."id" IN (1, 2, 3, 4, 5)
profiles.class
  # => Array

となり、結果はただの配列でActiveRecordの恩恵を受けられません。
素直にwhereで取得したほうがいいです。SQLも同じなので特に問題なさそうです。

profiles = Profile.where id: [1,2,3,4,5]
  # =>  SELECT "profiles".* FROM "profiles"  WHERE "profiles"."id" IN (1, 2, 3, 4, 5)
profiles.class
  # => Profile::ActiveRecord_Relation

Environment

pry(main)> Rails.version
=> "4.1.6"
metheglin
敷布団カバーと掛布団カバーを逆につけて寝ています
https://metheglin.jp/#logical
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away