“或”语句在Rails查询中不起作用

时间:2018-09-26 13:45:09

标签: ruby-on-rails ruby ruby-on-rails-5

我具有以下模型结构:

class Job < ApplicationRecord
     belongs_to :location
     has_and_belongs_to_many :users
end

class Location < ApplicationRecord
     has_and_belongs_to_many :users
     has_many :jobs
end

class User < ApplicationRecord
    has_and_belongs_to_many :locations
end

这使我可以为给定位置创建作业,然后将用户关联/分配给该作业。我的用户表具有active属性,当我尝试查询已经分配给作业或处于活动状态的用户时遇到错误。这是我的查询:

@users = @job.location.users.where(users: { id: @job.user_ids }).or(@job.location.users.where(active: true))

错误:

ArgumentError (Relation passed to #or must be structurally compatible. Incompatible values: [:references]):

谢谢。

1 个答案:

答案 0 :(得分:1)

我认为这应该可行:

@users = @job.location.users.where(id: @job.user_ids).or(@job.location.users.where(active: true))