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’)
or
scope :men, where(:sex => ‘M’)

scope :in_tights, where(“clothing_preference = ?”, ‘Tights’)
or
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

User.men

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

# get the men, order by the oldest first
User.men.oldest_first

# get the oldest man
User.men.oldest

# get the oldest man who has not been cancelled
User.men.oldest.not_cancelled

# get the users created in the first week of july
User.created_in_first_week_july_this_year

# get the dark hair colored men in tights created in the first week of july
User.men.in_tights.with_dark_hair.created_in_first_week_july_this_year

# Or you can use the last scope which references other scopes, to cut down on the tedium of chaining so many things:
User.old_dark_men_in_tights_for_july
[/sourcecode]