We are just starting Day 2. Mike Clark and Chad Fowler are giving the training and are really good at it. They are 24 people taking the training, most not from Denver, and one from Mexico. Right now attendees are not awake and Mike and Chad are trying to wake us up. Yesterday we covered routes, looked at the Rails and Mongrel source code, looked at a RESTful application, covered ActiveResource, and checked ActiveRecord Associations. It’s nice to be able to sit back and take time to play with all these goodies without having to deliver code. It’s a nice refresher for me. Next step will be meta programming.
Join Model: has_many :through
Polymorphic Associations: has_many :address, :as => :addressable
Custom Finders:
class User
has_many :visits do
def recent(limiit – 5)
find(:all, :order => ‘created_at DESC’, :limit => 5)
end
end
Active Record Scoping:
with_scope # protected now
before_filter :find_account
Scoped Relationships:
@event.registations.find(params[:id])
@user.events.find_by_id(params[:id])
@event.registations.find(:all, :conditions => “pre_register is true”)
Named Associations:
class Event < ActiveRecord::Base
has_many :registrations
has_many :pre_registrations,
:class_name => “Registration”,
:conditions => “pre_register is true”
end
@event.pre_registrations
Named Scope:
class Coupon < ActiveRecord::Base
named_scope :limit_not_exceeded, :conditions => “use_count < max_uses”
named_scope :usable_in_store, :conditions => “external_only is false”
end
Coupon.limit_not_exceeded
Coupon.usable_in_store
Coupon.limit_not_exceeded.usable_in_store
Dynamic Named Scope:
class Coupon < ActiveRecord::Base
named_scope :not_expired, lambda { { :conditions => [‘expires_at > ?’, Time.now] } }
named_scope :used_at_most, lambda { |uses| { :conditions => [‘use_count <= ?’, uses] } }
end
Coupon.not_expired
Coupon.used_at_most(30)
Coupon.not_expired.used_at_most(10)