简体   繁体   English

从belongs_to表获取Ruby中的对象集合时出错

[英]Error getting a collection of objects in Ruby from a belongs_to table

In this example, I'm trying to get a collection of Workouts that have been created by specific types of users (a Workout belongs to a specific User, has column name user_id): either the current_user, a Professional user (inherits from User), and an admin. 在此示例中,我试图获取由特定类型的用户(锻炼属于特定用户,列名称为user_id)创建的锻炼的集合:current_user,Professional用户(继承自User)和一位管理员。 Here is what I have right now: 这是我现在所拥有的:

Workout.where('(user_id= ?) OR (user_id= ?) OR (user_id= ?)', current_user.id, User.select(&:professional?), User.find_by_admin(true).id)

I believe the second item is throwing this error: 我相信第二项抛出此错误:

Mysql2::Error: Operand should contain 1 column(s): SELECT workouts .* FROM workouts WHERE ((user_id= 29) OR (user_id= 2,5,29) OR (user_id= 2)) ActiveRecord::StatementInvalid: Mysql2::Error: Operand should contain 1 column(s): SELECT workouts .* FROM workouts WHERE ((user_id= 29) OR (user_id= 2,5,29) OR (user_id= 2)) Mysql2 :: Error:操作数应包含1列:SELECT workouts 。* FROM workouts ((user_id = 29)或(user_id = 2,5,29)或(user_id = 2))的ActiveRecord :: StatementInvalid:Mysql2 ::错误:操作数应包含1列:SELECT workouts 。* FROM workouts ((user_id = 29)OR(user_id = 2,5,29)OR(user_id = 2))

If that's the case, how can I iterate over the user_id=2,5,29 (other than .each) and pull all the workouts that were created by this specific set of users? 如果是这样,我如何遍历user_id = 2,5,29(.each除外)并提取由该特定用户组创建的所有锻炼?

try this where query, which will look into array of id's for user_id match 试试这个where查询,它将查询user_id匹配的id数组

Workout.where(user_id: [current_user.id, User.select(&:professional?), User.find_by_admin(true).id].flatten)

Sql equivalent SQL等效

SELECT * FROM workouts WHERE user_id IN (1,3,6,4,2)

尝试这个

Workout.where(user_id: [current_user, User.select(&:professional), User.find_by_admin(true)]).map(&:id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CakePHP从类所属的表中获取数据 - CakePHP get data from a table that class belongs_to 连接“属于”另一个表的搜索结果 - Joining search results of a table that “belongs_to” another 所有和引用实际上在表中创建关系? - belongs_to and references actually create relations in table? 急于使用ActiveRecord从User表中加载单个条目以及相关的belong_to记录 - Eager loading individual entry from User table and associated belongs_to records using ActiveRecord Ruby on Rails:两种模型都有Has / Many和Belongs_to - Ruby on Rails: Two Models Both Have Has/Many and Belongs_to 如何在 ROR 中通过 belongs_to 与另一个表的关系对数据进行分组,并使组的 arrays 的名称是相关表中的名称 - How in ROR to group data by belongs_to relationships with another table and have the names of the group's arrays be names from the related table 如何从belongs_to关联模型范围内by_month? - How to scope by_month from belongs_to associated model? 通过named_scope返回对象数组 - has_many ... belongs_to association; UNION ALL查询 - Returning array of objects via named_scope — has_many…belongs_to association; UNION ALL query Rails 的 Activerecord 上的belongs_to 创建了大量查询 - belongs_to on Activerecord of Rails creates lots of queries Rails的归属关系vs父标识 - Rails belongs_to vs parent_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM