Home arrow Ruby-on-Rails arrow Updating and Deleting with the Active Record

Updating and Deleting with the Active Record

In this conclusion to a three-part series on the Rails framework's Active Record, you'll learn how to use dynamic finders, update and delete records, and more. This article is excerpted from chapter four of the book Beginning Rails: From Novice to Professional, written by Jeffrey Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
January 14, 2010
  1. · Updating and Deleting with the Active Record
  2. · Updating Records
  3. · Deleting Records
  4. · When Good Models Go Bad

print this article

Updating and Deleting with the Active Record
(Page 1 of 4 )

 Finding with Conditions

While finding a record by its primary key is useful, it requires that you know theidto begin with, which isn’t always the case. Sometimes you want to find records based on other criteria. This is where conditions come in to play. Conditions correspond to the SQLWHERE clause. If you want to find a record by its title, you pass a hash of options tofindwith a key of:conditionsand a value that contains either a hash of conditions or a SQL fragment.

Here, we’ll use a hash of conditions to indicate we want the first event with the title ofRailsConf.

>> Event.find(:first, :conditions => {:title => 'RailsConf'})
=> #<Event:0x25974c0 @attributes={"title"=>"RailsConf"...}>

Because we usedfind(:first), we’ll get only one record (the first one in the result set, even if there is more than one result). If we instead usefind(:all), we’ll get back a collection, even if the collection has only one item in it.

>> Event.find(:all, :conditions => "starts_at > '2007-01-01'")
=> [#<Event:0x24bbde4.>, #<Event:0x24bbde5.>, #<Event:0x24bbde6.>]

Notice the square brackets and remember that they indicate an array. More often than not, when you’re doing afind(:all)operation, you’re expecting more than one record in return. Butfind(:all)will always produce an array, even if that array is empty.

>> Event.find(:all, :conditions => {:title => 'Nonexistent'})
=> []

Using Dynamic Finders

It doesn’t get any easier than dynamic finders. We call these finders dynamic because they use Ruby’smethod_missingfunctionality to automatically create methods that don’t exist until runtime. Using dynamic finders, you can include the attribute you’re looking for directly in the method name. This will make more sense when you see it in action.

>> Event.find_by_title('RailsConf')
=> #<Event:0x2592b64 @attributes={"title"=>"RailsConf", ...}>

>> Event.find_by_occurs_on('2007-10-13')
=> #<Event:0x2554e90 @attributes={"occurs_on"=>"2007-10-13", ...}>

There are several variations of dynamic finders, which are summarized in the Table 4-2.

Table 4-2. Dynamic Finders  







find_by_title('RailsConf') # => Event


find_all_by_location('Canada') # => Array

find_by_*_and_*(cond1, cond2)

find_by_title_and_location('Party', 'Canada') # => Event

find_all_by_*_and_*(cond1, cond2)

find_all_by_title_and_location('Party', 'Canada') # => Array




blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials