2017-01-01 56 views
1

在我的控制器中,我正在寻找返回带有2个条件的数组。Rails重新调整具有多个条件的ID数组

parent_ids = StudentGuardian.where(:guardian_id => current_user.school_user.id).pluck(:student_id) 

classmodule_ids = SubjectStudent.pluck (:class_id) 

@homework = Homework.where("subject in ?", classmodule_ids) 

因此,在这种情况下,我需要找到从表中的学生证,然后我需要从另一个表中的类标识码。

然后我试图显示结果。

我可以同时进入一个查询吗?我也试过@homework = Homework.where("subject in ?", parent_ids, classmodule_ids)当然这不行!

回答

2

你只是想知道subject是在parent_ids还是classmodule_ids

所以你可以只合并parent_idsclassmodule_ids到一个数组:

# you could merge these a bunch of different ways, here's one: 
search_ids = (parent_ids + classmodule_ids).uniq 
# don't forget the() around ? below 
@homework = Homework.where("subject in (?)", search_ids) 

或者,如果由于某种原因你不想合并parent_idsclassmodule_ids

@homework = Homework.where("subject in (?) OR subject in (?)", parent_ids, classmodule_ids) 

但所有这一切将意味着subject也是一个ID ...是这样吗?