The following is a guest post by Ericka Ward and originally appeared on her blog. Ericka is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter here.
ActiveRecord provides the
where method to allow you to query your app’s database with specific conditions. The where method represents the
WHERE part of a SQL statement. You can pass the method a string, array, or hash. It is important to note that this method will always return an object that is an ActiveRecord::Relation, which looks and behaves like an array.
I am currently working with some Flatiron School classmates on a rails app that aggregates blog posts, tweets and github activity for a group of people called Teamline. We use the where method in our app on several occassions.
In the example below, we are querying our posts table to find all the blog posts that belong to specific person.
The code above is equivalent to the following SQL statement:
person_idwill replace the first question mark, start_date will replace the second and so on.
Again, this method will return an ActiveRecord::Relation object as well.
For more information on the many other ways you can use the
where method to perform conditional queries, check out the documentation:
Make yourself useful.