Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 556 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How would I check if a value is found in an array of values

#1
I want to perform an if condition where, if `linkedpub.LPU_ID` is found in an array of values(`@associated_linked_pub`), do some action.

I tried the following but the syntax is not correct.

Any suggestion is most welcomed..Thanks a lot

<% for linkedpub in Linkedpub.find(:all) %>
<% if linkedpub.LPU_ID IN @associated_linked_pub %>
# do action
<%end%>
<%end%>
Reply

#2
linkedpub.LPU_ID.in?(@associated_linked_pub.collect(&:id))

Using `in?` in these cases has always felt more natural to me.
Reply

#3
@associated_linked_pub.collect(&:id).include?(linkedpub.LPU_ID)
Reply

#4
You can use `Array#include?`

So...

if @associated_linked_pub.include? linkedpub.LPU_ID
...

**Edit**:

If `@associated_linked_pub` is a list of ActiveRecord objects then try this instead:

if @associated_linked_pub.map{|a| a.id}.include? linkedpub.LPU_ID
...


**Edit**:

Looking at your question in more detail, it looks like what you are doing is VERY inefficient and unscalable. Instead you could do...

For Rails 3.0:

Linkedpub.where(:id => @associated_linked_pub)

For Rails 2.x:

LinkedPub.find(:all, :conditions => { :id => @associated_linked_pub })

Rails will automatically create a SQL IN query such as:

SELECT * FROM linkedpubs WHERE id IN (34, 6, 2, 67, 8)
Reply

#5
if `@associated_linked_pub` is an array, try

if @associated_linked_pub.include?(linkedpub.LPU_ID)

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through