ActiveRecord Associations and the Proxy Pattern

Location: E145 - E146 Level: Expert
Average rating: ****.
(4.91, 11 ratings)


  • Survey of ActiveRecord Associations
  • What is the Proxy Pattern?
  • How to implement the Proxy Pattern in Ruby
    • The “Blank Slate”—undefining or delegating instance methods.
      • gotcha: use append_features to ensure other people’s metaprograms don’t clobber you.
  • What is with_scope?
    • Explanation
    • Close look at source code
    • Example enhancement: how to add group_by support.
  • Thesis: an association is an objectification of a with_scope
  • ActiveRecord Associations
    • Structure of the Rails Association source code (class hierarchy, file layout)
    • Use of the Proxy Pattern
    • Use of with_scope
    • Gotchas
      • shadowed methods
      • build modifies the proxy_target
    • Advanced functionality
      • after_add callbacks
      • extending associations with anonymous modules
        • real-world examples
          • overriding #build
          • defining a custom [] operator
          • implementing access control on the collection
        • Ruby details
          • super semantics in this context
          • #self.extended in this context
            • #alias_method_chain in this context
          • Mucking with the reflection object in STI subclasses, to add subclass module extensions
      • getting a pointer to the owner of the association with proxy_owner
      • defining class methods on the proxy_reflection.klass
      • single-quoted :conditions for variable where clauses in your SQL
      • Rolling your own enhancements: examples
        • Making “has_many :friends” automatically define a #has_friend? method
        • How will_paginate adds the paginate method to `has_many` and `has_many :through`
  • Using the Proxy Pattern outside of AR Associations
    • How HasFinder uses proxies for arbitrary find queries
      • Use of the Chain of Responsibility pattern to implement query composition
      • Delegation vs. undefining instance methods
      • How MethodChain uses proxies for lightweight method chain-like syntax (e.g., lambda { … }.should change {x}.from(1).to(2))
        • Instance_eval and its relation to the Proxy Pattern.

Nick Kallen

Pivotal Labs

Nick Kallen has been developing Ruby on Rails applications for two years at Pivotal Labs in San Francisco. He is a prolific blogger on Rails idioms and Ruby metaprogramming tricks, and is the author of the popular HasFinder gem.

News and Coverage
co-presented by Ruby Central, Inc. O'Reilly
  • Engine Yard
  • Sun Microsystems
  • FiveRuns
  • GotThingsDone
  • Heroku
  • ThoughtWorks
  • Atlantic Dominion Solutions
  • Blue Box Group
  • CodeGear
  • E-xact
  • ELC Technologies
  • EnterpriseDB
  • GemStone Systems
  • Intridea
  • Morph Labs
  • RightScale
  • TechRepublic

Sponsorship Opportunities

For information on exhibition and sponsorship opportunities at RailsConf, contact Yvonne Romaine.

Download the RailsConf Sponsor/Exhibitor Prospectus

Media and Promotional Opportunities

Download the Media & Promotional Partner Brochure (PDF) for more information on trade opportunities with O'Reilly conferences, or contact mediapartners@

Program Ideas

Post your suggestions for speakers, topics, and activities on the RailsConf wiki or send an email to

Press and Media

For media-related inquiries, contact

Contact Us

View a complete list of RailsConf 2008 contacts.