diff options
author | Vijay Dev <vijaydev.cse@gmail.com> | 2012-11-17 01:50:49 +0530 |
---|---|---|
committer | Vijay Dev <vijaydev.cse@gmail.com> | 2012-11-17 01:50:49 +0530 |
commit | 7b70eeed43045dc29e73e23fbfdc323e9d397026 (patch) | |
tree | 8c60cd5893f7e2d231130b7c0abdc0aee8e3bec7 /activerecord/lib/active_record/associations.rb | |
parent | 8eefdb6d7056dc0d4d63a5c34a4b12701ba21c88 (diff) | |
parent | 1fd008cd44cd2eea37db57ee6b3c17d3585d88c1 (diff) | |
download | rails-7b70eeed43045dc29e73e23fbfdc323e9d397026.tar.gz rails-7b70eeed43045dc29e73e23fbfdc323e9d397026.tar.bz2 rails-7b70eeed43045dc29e73e23fbfdc323e9d397026.zip |
Merge branch 'master' of github.com:lifo/docrails
Conflicts:
actionpack/lib/action_dispatch/routing/redirection.rb
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rw-r--r-- | activerecord/lib/active_record/associations.rb | 106 |
1 files changed, 53 insertions, 53 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 5949269f2a..651b17920c 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -308,11 +308,11 @@ module ActiveRecord # end # class Programmer < ActiveRecord::Base # has_many :assignments - # has_many :projects, :through => :assignments + # has_many :projects, through: :assignments # end # class Project < ActiveRecord::Base # has_many :assignments - # has_many :programmers, :through => :assignments + # has_many :programmers, through: :assignments # end # # For the second way, use +has_and_belongs_to_many+ in both models. This requires a join table @@ -429,7 +429,7 @@ module ActiveRecord # object from an association collection. # # class Project - # has_and_belongs_to_many :developers, :after_add => :evaluate_velocity + # has_and_belongs_to_many :developers, after_add: :evaluate_velocity # # def evaluate_velocity(developer) # ... @@ -440,7 +440,7 @@ module ActiveRecord # # class Project # has_and_belongs_to_many :developers, - # :after_add => [:evaluate_velocity, Proc.new { |p, d| p.shipping_date = Time.now}] + # after_add: [:evaluate_velocity, Proc.new { |p, d| p.shipping_date = Time.now}] # end # # Possible callbacks are: +before_add+, +after_add+, +before_remove+ and +after_remove+. @@ -510,7 +510,7 @@ module ActiveRecord # # class Author < ActiveRecord::Base # has_many :authorships - # has_many :books, :through => :authorships + # has_many :books, through: :authorships # end # # class Authorship < ActiveRecord::Base @@ -526,7 +526,7 @@ module ActiveRecord # # class Firm < ActiveRecord::Base # has_many :clients - # has_many :invoices, :through => :clients + # has_many :invoices, through: :clients # end # # class Client < ActiveRecord::Base @@ -546,7 +546,7 @@ module ActiveRecord # # class Group < ActiveRecord::Base # has_many :users - # has_many :avatars, :through => :users + # has_many :avatars, through: :users # end # # class User < ActiveRecord::Base @@ -574,7 +574,7 @@ module ActiveRecord # works correctly (where <tt>tags</tt> is a +has_many+ <tt>:through</tt> association): # # @post = Post.first - # @tag = @post.tags.build :name => "ruby" + # @tag = @post.tags.build name: "ruby" # @tag.save # # The last line ought to save the through record (a <tt>Taggable</tt>). This will only work if the @@ -582,7 +582,7 @@ module ActiveRecord # # class Taggable < ActiveRecord::Base # belongs_to :post - # belongs_to :tag, :inverse_of => :taggings + # belongs_to :tag, inverse_of: :taggings # end # # == Nested Associations @@ -592,8 +592,8 @@ module ActiveRecord # # class Author < ActiveRecord::Base # has_many :posts - # has_many :comments, :through => :posts - # has_many :commenters, :through => :comments + # has_many :comments, through: :posts + # has_many :commenters, through: :comments # end # # class Post < ActiveRecord::Base @@ -611,12 +611,12 @@ module ActiveRecord # # class Author < ActiveRecord::Base # has_many :posts - # has_many :commenters, :through => :posts + # has_many :commenters, through: :posts # end # # class Post < ActiveRecord::Base # has_many :comments - # has_many :commenters, :through => :comments + # has_many :commenters, through: :comments # end # # class Comment < ActiveRecord::Base @@ -635,11 +635,11 @@ module ActiveRecord # must adhere to. # # class Asset < ActiveRecord::Base - # belongs_to :attachable, :polymorphic => true + # belongs_to :attachable, polymorphic: true # end # # class Post < ActiveRecord::Base - # has_many :assets, :as => :attachable # The :as option specifies the polymorphic interface to use. + # has_many :assets, as: :attachable # The :as option specifies the polymorphic interface to use. # end # # @asset.attachable = @post @@ -656,7 +656,7 @@ module ActiveRecord # column in the posts table. # # class Asset < ActiveRecord::Base - # belongs_to :attachable, :polymorphic => true + # belongs_to :attachable, polymorphic: true # # def attachable_type=(sType) # super(sType.to_s.classify.constantize.base_class.to_s) @@ -664,8 +664,8 @@ module ActiveRecord # end # # class Post < ActiveRecord::Base - # # because we store "Post" in attachable_type now :dependent => :destroy will work - # has_many :assets, :as => :attachable, :dependent => :destroy + # # because we store "Post" in attachable_type now dependent: :destroy will work + # has_many :assets, as: :attachable, dependent: :destroy # end # # class GuestPost < Post @@ -727,7 +727,7 @@ module ActiveRecord # # To include a deep hierarchy of associations, use a hash: # - # Post.includes(:author, {:comments => {:author => :gravatar}}).each do |post| + # Post.includes(:author, {comments: {author: :gravatar}}).each do |post| # # That'll grab not only all the comments but all their authors and gravatar pictures. # You can mix and match symbols, arrays and hashes in any combination to describe the @@ -752,13 +752,13 @@ module ActiveRecord # In the above example posts with no approved comments are not returned at all, because # the conditions apply to the SQL statement as a whole and not just to the association. # You must disambiguate column references for this fallback to happen, for example - # <tt>:order => "author.name DESC"</tt> will work but <tt>:order => "name DESC"</tt> will not. + # <tt>order: "author.name DESC"</tt> will work but <tt>order: "name DESC"</tt> will not. # # If you do want eager load only some members of an association it is usually more natural # to include an association which has conditions defined on it: # # class Post < ActiveRecord::Base - # has_many :approved_comments, -> { where approved: true }, :class_name => 'Comment' + # has_many :approved_comments, -> { where approved: true }, class_name: 'Comment' # end # # Post.includes(:approved_comments) @@ -770,7 +770,7 @@ module ActiveRecord # returning all the associated objects: # # class Picture < ActiveRecord::Base - # has_many :most_recent_comments, -> { order('id DESC').limit(10) }, :class_name => 'Comment' + # has_many :most_recent_comments, -> { order('id DESC').limit(10) }, class_name: 'Comment' # end # # Picture.includes(:most_recent_comments).first.most_recent_comments # => returns all associated comments. @@ -778,7 +778,7 @@ module ActiveRecord # Eager loading is supported with polymorphic associations. # # class Address < ActiveRecord::Base - # belongs_to :addressable, :polymorphic => true + # belongs_to :addressable, polymorphic: true # end # # A call that tries to eager load the addressable model @@ -812,10 +812,10 @@ module ActiveRecord # # TreeMixin.joins(:children) # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ... - # TreeMixin.joins(:children => :parent) + # TreeMixin.joins(children: :parent) # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ... # INNER JOIN parents_mixins ... - # TreeMixin.joins(:children => {:parent => :children}) + # TreeMixin.joins(children: {parent: :children}) # # => SELECT ... FROM mixins INNER JOIN mixins childrens_mixins ... # INNER JOIN parents_mixins ... # INNER JOIN mixins childrens_mixins_2 @@ -824,10 +824,10 @@ module ActiveRecord # # Post.joins(:categories) # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ... - # Post.joins(:categories => :posts) + # Post.joins(categories: :posts) # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ... # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories - # Post.joins(:categories => {:posts => :categories}) + # Post.joins(categories: {posts: :categories}) # # => SELECT ... FROM posts INNER JOIN categories_posts ... INNER JOIN categories ... # INNER JOIN categories_posts posts_categories_join INNER JOIN posts posts_categories # INNER JOIN categories_posts categories_posts_join INNER JOIN categories categories_posts_2 @@ -871,7 +871,7 @@ module ActiveRecord # # module Billing # class Account < ActiveRecord::Base - # belongs_to :firm, :class_name => "MyApplication::Business::Firm" + # belongs_to :firm, class_name: "MyApplication::Business::Firm" # end # end # end @@ -913,16 +913,16 @@ module ActiveRecord # example, if we changed our model definitions to: # # class Dungeon < ActiveRecord::Base - # has_many :traps, :inverse_of => :dungeon - # has_one :evil_wizard, :inverse_of => :dungeon + # has_many :traps, inverse_of: :dungeon + # has_one :evil_wizard, inverse_of: :dungeon # end # # class Trap < ActiveRecord::Base - # belongs_to :dungeon, :inverse_of => :traps + # belongs_to :dungeon, inverse_of: :traps # end # # class EvilWizard < ActiveRecord::Base - # belongs_to :dungeon, :inverse_of => :evil_wizard + # belongs_to :dungeon, inverse_of: :evil_wizard # end # # Then, from our code snippet above, +d+ and <tt>t.dungeon</tt> are actually the same @@ -945,7 +945,7 @@ module ActiveRecord # For example: # # class Author - # has_many :posts, :dependent => :destroy + # has_many :posts, dependent: :destroy # end # Author.find(1).destroy # => Will destroy all of the author's posts, too # @@ -1029,12 +1029,12 @@ module ActiveRecord # parent object. # [collection.delete(object, ...)] # Removes one or more objects from the collection by setting their foreign keys to +NULL+. - # Objects will be in addition destroyed if they're associated with <tt>:dependent => :destroy</tt>, - # and deleted if they're associated with <tt>:dependent => :delete_all</tt>. + # Objects will be in addition destroyed if they're associated with <tt>dependent: :destroy</tt>, + # and deleted if they're associated with <tt>dependent: :delete_all</tt>. # # If the <tt>:through</tt> option is used, then the join records are deleted (rather than - # nullified) by default, but you can specify <tt>:dependent => :destroy</tt> or - # <tt>:dependent => :nullify</tt> to override this. + # nullified) by default, but you can specify <tt>dependent: :destroy</tt> or + # <tt>dependent: :nullify</tt> to override this. # [collection.destroy(object, ...)] # Removes one or more objects from the collection by running <tt>destroy</tt> on # each record, regardless of any dependent option, ensuring callbacks are run. @@ -1052,8 +1052,8 @@ module ActiveRecord # method loads the models and calls <tt>collection=</tt>. See above. # [collection.clear] # Removes every object from the collection. This destroys the associated objects if they - # are associated with <tt>:dependent => :destroy</tt>, deletes them directly from the - # database if <tt>:dependent => :delete_all</tt>, otherwise sets their foreign keys to +NULL+. + # are associated with <tt>dependent: :destroy</tt>, deletes them directly from the + # database if <tt>dependent: :delete_all</tt>, otherwise sets their foreign keys to +NULL+. # If the <tt>:through</tt> option is true no destroy callbacks are invoked on the join models. # Join models are directly deleted. # [collection.empty?] @@ -1081,7 +1081,7 @@ module ActiveRecord # === Example # # Example: A Firm class declares <tt>has_many :clients</tt>, which will add: - # * <tt>Firm#clients</tt> (similar to <tt>Clients.all :conditions => ["firm_id = ?", id]</tt>) + # * <tt>Firm#clients</tt> (similar to <tt>Clients.all conditions: ["firm_id = ?", id]</tt>) # * <tt>Firm#clients<<</tt> # * <tt>Firm#clients.delete</tt> # * <tt>Firm#clients.destroy</tt> @@ -1091,8 +1091,8 @@ module ActiveRecord # * <tt>Firm#clients.clear</tt> # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>) # * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>) - # * <tt>Firm#clients.find</tt> (similar to <tt>Client.find(id, :conditions => "firm_id = #{id}")</tt>) - # * <tt>Firm#clients.exists?(:name => 'ACME')</tt> (similar to <tt>Client.exists?(:name => 'ACME', :firm_id => firm.id)</tt>) + # * <tt>Firm#clients.find</tt> (similar to <tt>Client.find(id, conditions: "firm_id = #{id}")</tt>) + # * <tt>Firm#clients.exists?(name: 'ACME')</tt> (similar to <tt>Client.exists?(name: 'ACME', firm_id: firm.id)</tt>) # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new("firm_id" => id)</tt>) # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new("firm_id" => id); c.save; c</tt>) # The declaration can also include an options hash to specialize the behavior of the association. @@ -1149,7 +1149,7 @@ module ActiveRecord # [:source] # Specifies the source association name used by <tt>has_many :through</tt> queries. # Only use it if the name cannot be inferred from the association. - # <tt>has_many :subscribers, :through => :subscriptions</tt> will look for either <tt>:subscribers</tt> or + # <tt>has_many :subscribers, through: :subscriptions</tt> will look for either <tt>:subscribers</tt> or # <tt>:subscriber</tt> on Subscription, unless a <tt>:source</tt> is given. # [:source_type] # Specifies type of the source association used by <tt>has_many :through</tt> queries where the source @@ -1213,7 +1213,7 @@ module ActiveRecord # === Example # # An Account class declares <tt>has_one :beneficiary</tt>, which will add: - # * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.first(:conditions => "account_id = #{id}")</tt>) + # * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.first(conditions: "account_id = #{id}")</tt>) # * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>) # * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new("account_id" => id)</tt>) # * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new("account_id" => id); b.save; b</tt>) @@ -1253,7 +1253,7 @@ module ActiveRecord # [:source] # Specifies the source association name used by <tt>has_one :through</tt> queries. # Only use it if the name cannot be inferred from the association. - # <tt>has_one :favorite, :through => :favorites</tt> will look for a + # <tt>has_one :favorite, through: :favorites</tt> will look for a # <tt>:favorite</tt> on Favorite, unless a <tt>:source</tt> is given. # [:source_type] # Specifies type of the source association used by <tt>has_one :through</tt> queries where the source @@ -1273,11 +1273,11 @@ module ActiveRecord # See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail. # # Option examples: - # has_one :credit_card, :dependent => :destroy # destroys the associated credit card - # has_one :credit_card, :dependent => :nullify # updates the associated records foreign + # has_one :credit_card, dependent: :destroy # destroys the associated credit card + # has_one :credit_card, dependent: :nullify # updates the associated records foreign # # key value to NULL rather than destroying it - # has_one :last_comment, -> { order 'posted_on' }, :class_name => "Comment" - # has_one :project_manager, -> { where role: 'project_manager' }, :class_name => "Person" + # has_one :last_comment, -> { order 'posted_on' }, class_name: "Comment" + # has_one :project_manager, -> { where role: 'project_manager' }, class_name: "Person" # has_one :attachment, as: :attachable # has_one :boss, readonly: :true # has_one :club, through: :membership @@ -1332,12 +1332,12 @@ module ActiveRecord # Specify the foreign key used for the association. By default this is guessed to be the name # of the association with an "_id" suffix. So a class that defines a <tt>belongs_to :person</tt> # association will use "person_id" as the default <tt>:foreign_key</tt>. Similarly, - # <tt>belongs_to :favorite_person, :class_name => "Person"</tt> will use a foreign key + # <tt>belongs_to :favorite_person, class_name: "Person"</tt> will use a foreign key # of "favorite_person_id". # [:foreign_type] # Specify the column used to store the associated object's type, if this is a polymorphic # association. By default this is guessed to be the name of the association with a "_type" - # suffix. So a class that defines a <tt>belongs_to :taggable, :polymorphic => true</tt> + # suffix. So a class that defines a <tt>belongs_to :taggable, polymorphic: true</tt> # association will use "taggable_type" as the default <tt>:foreign_type</tt>. # [:primary_key] # Specify the method that returns the primary key of associated object used for the association. @@ -1357,7 +1357,7 @@ module ActiveRecord # <tt>#{table_name}_count</tt> is created on the associate class (such that Post.comments_count will # return the count cached, see note below). You can also specify a custom counter # cache column by providing a column name instead of a +true+/+false+ value to this - # option (e.g., <tt>:counter_cache => :my_custom_counter</tt>.) + # option (e.g., <tt>counter_cache: :my_custom_counter</tt>.) # Note: Specifying a counter cache will add it to that model's list of readonly attributes # using +attr_readonly+. # [:polymorphic] @@ -1415,7 +1415,7 @@ module ActiveRecord # # class CreateDevelopersProjectsJoinTable < ActiveRecord::Migration # def change - # create_table :developers_projects, :id => false do |t| + # create_table :developers_projects, id: false do |t| # t.integer :developer_id # t.integer :project_id # end |