You are here

find

Rails find :conditions

The Class ActiveRecord::Base provides an interesting method to execute searches on the database without the need to write SQL queries. It is the find method.

The find method has some retrieval options (:all, :first, :last, :id) and many parameters which are detailed in the links above. In this article we'd like to focus on the :conditions parameter.

Search an Array in Ruby

If you want to search for a value in an array in Ruby you could do something like this:
 
array = ['Bart','Lisa','Maggie'] array.include?('Homer') # returns false array.include?('Lisa') # returns true
 
Another possibility is to do this:
 
array = ['Bart','Lisa','Maggie'] array.index 'Homer' # returns nil array.index 'Lisa' # returns 1
 
Both methods should work for searching an array.

Subscribe to RSS - find