2011-07-15 35 views
0

我在extJS中有2个多选择器(受访者组和被调查者)。 Erery受访者拥有一个团体(仅1人)。如何收集和比较IDS?

这里,我如何选择我的IDS ...

我得到respondent_group ID,例如= 1

respondent_ids  = params[:selected_respondents].split(/,/).collect {|r| r.to_i} 

在这里我得到受访者的ID:3,4,1

respondent_pure_ids = params[:selected_alone_respondents].split(/,/).collect {|r| r.to_i} 

我在这里找到组中的受访者(例如我有10个组,每个组有1-3个受访者)。

respondents  = Respondent.find(:all, :conditions => ["respondent_group_id in (?) AND id NOT IN (?)", respondent_ids, session[:user].id]) 

我找到答卷者。

respondents_alone = Respondent.find(:all, :conditions => ["id in (?) AND id NOT IN (?)", respondent_pure_ids, session[:user].id]) 

在这里我发现受访者(我找到id respondent_group =?)并发送给他们电子邮件。

 respondents.each do |r| 
      Notifier.deliver_inquiry_notification() 
     end 

我想要什么? 我得到了respondentsrespondents_alone id。

For example respondents = 3 , 4 , 6 
respondents_ alone = 3, 5, 6, 8 

我在两个中都有3个和6个ID。我不想公布我的数据。如何检查:如果受访者ID不等于respondent_alone ID我发送电子邮件其他错误!

回答

2

您可以使用红宝石数组运算得到两个阵列之间的差别还是让每个条目只有一次:

a = [3, 4, 6] 
b = [3, 5, 6, 8] 
a - b # [4] (only a without any element of b, difference) 
b - a # [5, 8] (only b without any element of a, difference) 
a | b # [3, 4, 6, 5, 8] (complete set of all ids, union) 
a & b # [3, 6] (ids same in both arrays, intersection) 

有了这个,你可以检查一些ID仅在一个阵列或两个,例如差异为空或a|b==a&b =>两者相等

比较http://www.ruby-doc.org/core/classes/Array.html