Latest Tweet
- Dropped container of polynesian sauce on floor, fully-open side down. NOTHING CAME OUT. Chick-fil-A, champion of viscous sauces. 5 days ago
- More updates...
Categories
Tags
academia alcohol animals boyfriend cooking databases email forwards family Flickr food friends health Javascript Lexington Linux list Mario math movies music news OS X Perl PHP politics programming quizzes Rails rants reading Ruby screenshots shopping Sims sports themes tutorials Twilight is ridiculous vehicles video games videos weather Web development work zombies again-
Recent Comments
-
Random Quote
First get your facts; then you can distort them at your leisure.
— Mark Twain Syndication
All posts RSS feed
the many methods to #find things in Rails
For the longest time, I didn’t understand the full power of the various #find methods in Rails. I probably still don’t, but my understanding of them has certainly expanded. I used to use plain #find for everything. If I wanted to find all rows in the table ‘groups’ that had an ‘id’ field value of 1, 2, 3, or 4, I would do something like this:
Ruby
:all,
:conditions => ["id=? OR id=? OR id=? OR ID=?", 1, 2, 3, 4]
)
Now, while this is a perfectly valid way to find those rows, it’s a little too verbose. There’s a cleaner, shorter way to get the same result:
Ruby
This works because #find can take any of the following as primary ID’s to look up:
Ruby
Ruby
Ruby
Ruby
Beyond the regular #find method, there are also auto-generated methods for each of your Rails models. Say you have a UserGroup model that associates a user to a group (i.e. group membership). The table ‘users_groups’ has the fields ‘id’, ‘user_id’, and ‘group_id’. What’s an easy way to find all users belonging to the group with ID #3?
Ruby
With that, you’re telling the UserGroup model to find all rows based on the group ID, which you give it as the first parameter. You’re also telling it to eagerly load the associated rows from the ‘users’ table, ordering the results by the user’s name. Then, you’re using #map on the returned array to grab just the results of each row’s #user method. Thus, you end up with an array of User objects, all of whom belong to the group with ID #3.
If you just want to 1) find a single row and 2) have the single row returned (as opposed to an array with that row as its only element), use a #find_by_FIELD_NAME method, instead of a #find_all_by_FIELD_NAME:
Ruby