Ruby on rails Rails-HABTM关联,是否存在检查?数组中任何对象的

Ruby on rails Rails-HABTM关联,是否存在检查?数组中任何对象的,ruby-on-rails,associations,has-and-belongs-to-many,Ruby On Rails,Associations,Has And Belongs To Many,我想检查数组中是否存在任何HABTM关系,如果存在,则返回true 目前,我能看到的唯一方法是: result = false [1,2,3,4].each do |i| if user.parents.exists?(i) result = true break end end 我尝试按如下方式传入一个数组,但得到一个异常 result = true if user.parents.exists?([1,2,3,4]) NoMethodError: undefine

我想检查数组中是否存在任何HABTM关系,如果存在,则返回true

目前,我能看到的唯一方法是:

result = false
[1,2,3,4].each do |i|
  if user.parents.exists?(i)
    result = true
    break
  end
end
我尝试按如下方式传入一个数组,但得到一个异常

result = true if user.parents.exists?([1,2,3,4])

NoMethodError: undefined method `include?` for 1:Fixnum
有更好的方法吗

[1,2,3,4].inject(false) {|res, i| res ||= user.parents.exists?(i)}
几乎相同的逻辑,只是更多的ruby-ish代码使用注入语法

更新:

我还没有测试过。但这也可能奏效:

user.parents.exists?(:id => [1,2,3,4])

更新后的版本有效。与使用inject相比,我更喜欢它,它更具可读性。谢谢你