Some Arel Notes

I talked to a gentleman in #rubyonrails on IRC who goes by the name of codepoet_ about Rails3 and Arel for a while today and he linked me to what is somewhat of an Arel cheat sheet he created.

With his permission I am reposting this as a reference tool for myself.  Hopefully someone else can benefit!
[sourcecode language=’ror’]
# In a User model:

scope :men, where(“sex = ?”, ‘M’)
scope :men, where(:sex => ‘M’)

scope :in_tights, where(“clothing_preference = ?”, ‘Tights’)
scope :in_tights, where(“clothing_preference, ‘Tights’)

scope :created_in_first_week_july_this_year, where(“DATE(created_at) BETWEEN ? AND ?”, Date.parse(“July”), Date.parse(“July”) + 7.days)

scope :with_dark_hair, where(“(hair_color = ? OR ?)”, “black”, “brown”)

scope :oldest_first, order(“age DESC”)

scope :oldest, oldest_first.limit(1)

scope :not_cancelled, where(“status != ‘Cancelled'”)

scope :old_dark_men_in_tights_for_july, men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# get the men

#get the men in tights (these two do the same thing)

# get the men, order by the oldest first

# get the oldest man

# get the oldest man who has not been cancelled

# get the users created in the first week of july

# get the dark hair colored men in tights created in the first week of july

# Or you can use the last scope which references other scopes, to cut down on the tedium of chaining so many things: