mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Add a clearer example for with_options (lifted from the Active Support guide).
This commit is contained in:
committed by
Mani Tadayon
parent
74b44dfb86
commit
797af0faa5
@@ -7,13 +7,27 @@ class Object
|
||||
# provided. Each method called on the block variable must take an options
|
||||
# hash as its final argument.
|
||||
#
|
||||
# with_options :order => 'created_at', :class_name => 'Comment' do |post|
|
||||
# post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
|
||||
# post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
|
||||
# post.has_many :all_comments
|
||||
# Without with_options, this code contains duplication:
|
||||
#
|
||||
# class Account < ActiveRecord::Base
|
||||
# has_many :customers, :dependent => :destroy
|
||||
# has_many :products, :dependent => :destroy
|
||||
# has_many :invoices, :dependent => :destroy
|
||||
# has_many :expenses, :dependent => :destroy
|
||||
# end
|
||||
#
|
||||
# Can also be used with an explicit receiver:
|
||||
# Using with_options, we can remove the duplication:
|
||||
#
|
||||
# class Account < ActiveRecord::Base
|
||||
# with_options :dependent => :destroy do |assoc|
|
||||
# assoc.has_many :customers
|
||||
# assoc.has_many :products
|
||||
# assoc.has_many :invoices
|
||||
# assoc.has_many :expenses
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# It also be used with an explicit receiver:
|
||||
#
|
||||
# map.with_options :controller => "people" do |people|
|
||||
# people.connect "/people", :action => "index"
|
||||
|
||||
Reference in New Issue
Block a user