I’ve been trying to read more code lately and figured that I’d be best served by reading code I use frequently. Here are some notes of things I gathered from reading the ActiveRecord base.rb file.
1. Handling large query parameter sets
You can do safe queries by specifying your query arguments as a has, rather than an array using ‘?’.
Company.find(:first, :conditions => [
"id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
{ :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
])
2. Querying ranges
Student.find(:all, :conditions => { :grade => 9..12 })
This will yield a “BETWEEN 9 AND 12″ in the query string
Student.find(:all, :conditions => { :grade => [9,11,12] })
This will yield an “IN (9, 11, 12)” in the query string
3. [] notation on ActiveRecord objections
I’ve used [] on ActiveRecord objects to get the id from the attributes hash, rather than the object_id, which can pull some useless number off of the ruby object. What I didn’t know was that the following sets were calls to the same method:
@object[:attribute]
@object.read_attribute(:attribute)
and
@object[:attribute]= "abc"
@object.write_attribute(:attribute, "abc").
4. Attribute presence checks
You can test if an attribute is nil or blank by doing the normal method call with an appended ‘?’. Here’s an example from the codebase:
user = User.new(:name => "David") user.name? # => true anonymous = User.new(:name => "") anonymous.name? # => false
5. Attributes before they are typecast
A call like this will give you back the raw data before it is cast to whatever is expected (like an integer).
@object.id_before_type_cast
6. Find or initialize by
This is just like ActiveRecord::Base#find_or_create_by… methods, except it calls #new instead of #create.
7. Serializing Arrays, Hashes, and Objects
I didn’t realize that you could serialize objects under other objects in ActiveRecord. You can. It serializes your object or hash to YAML and saves it as text in the database.
class User { "background" => "black", "display" => large })
User.find(user.id).preferences # => { "background" => "black", "display" => large }
8. Protected attributes
Protected attributes are attributes protected from mass assignment. In other words, if your :name attribute is protected, then the following code will initialize a User without setting the :name attribute.
class User "George") user.name #=> nil





Pingback: uberVU - social comments
Pingback: More ActiveRecord Notes
Pingback: Active Record Find Methods « Sean Behan's Blog