我一直在挖掘,但我找不到对我有意义的答案。我有一个单独的“组”表,它只是一个id和一个名字。我有其他表以各种方式使用这些组。
GROUPS {id, name}
INCIDENTS {id, reportedby_group_id, assignee_group_id, resolving_group_id}
我的模型设置如此:
class Group < ActiveRecord::Base
has_many :incidents
end
class Incident < ActiveRecord::Base
belongs_to :reportedby_group, :class_name => 'Group', :foreign_key => 'reportedby_group_id'
belongs_to :resolving_group, :class_name => 'Group', :foreign_key => 'resolving_group_id'
belongs_to :assignee_group, :class_name => 'Group', :foreign_key => 'assignee_group_id'
end
我想做的是分别查询包含某些“解决群组”,“受让人群组”等的所有事件,例如:
Incident.joins(:resolving_group).where(:resolving_group => {:name => groups(arr)})
但是,当我尝试执行上述查询时,无法将Group.name与解析组关联,它会引出以下查询:
SELECT `incidents`.* FROM `incidents` INNER JOIN `groups` ON `groups`.`id` = `incidents`.`resolving_group_id` WHERE `resolving_group`.`name` IN (
当我在找这样的东西时:
SELECT `incidents`.* FROM `incidents` INNER JOIN `groups` ON `groups`.`id` = `incidents`.`resolving_group_id` WHERE `group`.`name` IN (
答案 0 :(得分:1)
Incident.joins(:resolving_group).where('group.name' => groups)
不知道为什么以前没有发生过这件事。