How would I check if a value is found in an array of values
我想执行一个if条件,如果在一个值数组(
我试过以下方法,但语法不正确。
任何建议都是最受欢迎的……非常感谢
| 1 2 3 4 5 | <% for linkedpub in Linkedpub.find(:all) %> <% if linkedpub.LPU_ID IN @associated_linked_pub %> # do action <%end%> <%end%> | 
您可以使用
所以…
| 1 2 | if @associated_linked_pub.include? linkedpub.LPU_ID ... | 
编辑:
如果
| 1 2 | if @associated_linked_pub.map{|a| a.id}.include? linkedpub.LPU_ID ... | 
编辑:
更详细地看你的问题,你所做的似乎是非常低效和不可分割的。相反,你可以…
轨道3:
| 1 | Linkedpub.where(:id => @associated_linked_pub) | 
对于Rails 2 x:
| 1 | LinkedPub.find(:all, :conditions => { :id => @associated_linked_pub }) | 
Rails将自动在查询中创建SQL,例如:
| 1 | SELECT * FROM linkedpubs WHERE id IN (34, 6, 2, 67, 8) | 
| 1 | linkedpub.LPU_ID.in?(@associated_linked_pub.collect(&:id)) | 
在这些情况下使用
如果
| 1 | if @associated_linked_pub.include?(linkedpub.LPU_ID) | 
| 1 | @associated_linked_pub.collect(&:id).include?(linkedpub.LPU_ID) |