ActiveRecord Associations and the Proxy Pattern
- 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.
- The “Blank Slate”—undefining or delegating instance methods.
- 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
- real-world examples
- 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.
- How HasFinder uses proxies for arbitrary find queries
People planning to attend this session also want to see:
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.






















