Converting an array of objects to ActiveRecord::Relation

后端 未结 5 1421
醉酒成梦
醉酒成梦 2020-12-04 07:00

I have an array of objects, let\'s call it an Indicator. I want to run Indicator class methods (those of the def self.subjects variety, scopes, etc

5条回答
  •  再見小時候
    2020-12-04 07:46

    ActiveRecord::Relation binds database query which retrieves data from database.

    Suppose to make sense, We have array with objects of same class, then with which query we suppose to bind them?

    When I run,

    users = User.where(id: [1,3,4,5])
      User Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IN (1, 3, 4, 5)  ORDER BY created_at desc
    

    Here in above, usersreturn Relation object but binds database query behind it and you can view it,

    users.to_sql
     => "SELECT `users`.* FROM `users` WHERE `users`.`id` IN (1, 3, 4, 5)  ORDER BY created_at desc"
    

    So it is not possible to return ActiveRecord::Relation from array of objects which is independent of sql query.

提交回复
热议问题