Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
223f3117
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
223f3117
编写于
5月 19, 2011
作者:
S
Sebastian Martinez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove extra white-spaces.
上级
3b18e900
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
24 addition
and
24 deletion
+24
-24
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+24
-24
未找到文件。
activerecord/lib/active_record/associations.rb
浏览文件 @
223f3117
...
@@ -468,9 +468,9 @@ def association_instance_set(name, association)
...
@@ -468,9 +468,9 @@ def association_instance_set(name, association)
# === Association Join Models
# === Association Join Models
#
#
# Has Many associations can be configured with the <tt>:through</tt> option to use an
# Has Many associations can be configured with the <tt>:through</tt> option to use an
# explicit join model to retrieve the data.
This operates similarly to a
# explicit join model to retrieve the data. This operates similarly to a
# +has_and_belongs_to_many+ association.
The advantage is that you're able to add validations,
# +has_and_belongs_to_many+ association. The advantage is that you're able to add validations,
# callbacks, and extra attributes on the join model.
Consider the following schema:
# callbacks, and extra attributes on the join model. Consider the following schema:
#
#
# class Author < ActiveRecord::Base
# class Author < ActiveRecord::Base
# has_many :authorships
# has_many :authorships
...
@@ -527,7 +527,7 @@ def association_instance_set(name, association)
...
@@ -527,7 +527,7 @@ def association_instance_set(name, association)
# @group.avatars # selects all avatars by going through the User join model.
# @group.avatars # selects all avatars by going through the User join model.
#
#
# An important caveat with going through +has_one+ or +has_many+ associations on the
# An important caveat with going through +has_one+ or +has_many+ associations on the
# join model is that these associations are *read-only*.
For example, the following
# join model is that these associations are *read-only*. For example, the following
# would not work following the previous example:
# would not work following the previous example:
#
#
# @group.avatars << Avatar.new # this would work if User belonged_to Avatar rather than the other way around
# @group.avatars << Avatar.new # this would work if User belonged_to Avatar rather than the other way around
...
@@ -595,7 +595,7 @@ def association_instance_set(name, association)
...
@@ -595,7 +595,7 @@ def association_instance_set(name, association)
# === Polymorphic Associations
# === Polymorphic Associations
#
#
# Polymorphic associations on models are not restricted on what types of models they
# Polymorphic associations on models are not restricted on what types of models they
# can be associated with.
Rather, they specify an interface that a +has_many+ association
# can be associated with. Rather, they specify an interface that a +has_many+ association
# must adhere to.
# must adhere to.
#
#
# class Asset < ActiveRecord::Base
# class Asset < ActiveRecord::Base
...
@@ -609,7 +609,7 @@ def association_instance_set(name, association)
...
@@ -609,7 +609,7 @@ def association_instance_set(name, association)
# @asset.attachable = @post
# @asset.attachable = @post
#
#
# This works by using a type column in addition to a foreign key to specify the associated
# This works by using a type column in addition to a foreign key to specify the associated
# record.
In the Asset example, you'd need an +attachable_id+ integer column and an
# record. In the Asset example, you'd need an +attachable_id+ integer column and an
# +attachable_type+ string column.
# +attachable_type+ string column.
#
#
# Using polymorphic associations in combination with single table inheritance (STI) is
# Using polymorphic associations in combination with single table inheritance (STI) is
...
@@ -740,7 +740,7 @@ def association_instance_set(name, association)
...
@@ -740,7 +740,7 @@ def association_instance_set(name, association)
# Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments.
# Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments.
#
#
# When eager loaded, conditions are interpolated in the context of the model class, not
# When eager loaded, conditions are interpolated in the context of the model class, not
# the model instance.
Conditions are lazily interpolated before the actual model exists.
# the model instance. Conditions are lazily interpolated before the actual model exists.
#
#
# Eager loading is supported with polymorphic associations.
# Eager loading is supported with polymorphic associations.
#
#
...
@@ -764,7 +764,7 @@ def association_instance_set(name, association)
...
@@ -764,7 +764,7 @@ def association_instance_set(name, association)
# == Table Aliasing
# == Table Aliasing
#
#
# Active Record uses table aliasing in the case that a table is referenced multiple times
# Active Record uses table aliasing in the case that a table is referenced multiple times
# in a join.
If a table is referenced only once, the standard table name is used.
The
# in a join.
If a table is referenced only once, the standard table name is used.
The
# second time, the table is aliased as <tt>#{reflection_name}_#{parent_table_name}</tt>.
# second time, the table is aliased as <tt>#{reflection_name}_#{parent_table_name}</tt>.
# Indexes are appended for any more successive uses of the table name.
# Indexes are appended for any more successive uses of the table name.
#
#
...
@@ -846,7 +846,7 @@ def association_instance_set(name, association)
...
@@ -846,7 +846,7 @@ def association_instance_set(name, association)
# == Bi-directional associations
# == Bi-directional associations
#
#
# When you specify an association there is usually an association on the associated model
# When you specify an association there is usually an association on the associated model
# that specifies the same relationship in reverse.
For example, with the following models:
# that specifies the same relationship in reverse. For example, with the following models:
#
#
# class Dungeon < ActiveRecord::Base
# class Dungeon < ActiveRecord::Base
# has_many :traps
# has_many :traps
...
@@ -863,9 +863,9 @@ def association_instance_set(name, association)
...
@@ -863,9 +863,9 @@ def association_instance_set(name, association)
#
#
# The +traps+ association on +Dungeon+ and the +dungeon+ association on +Trap+ are
# The +traps+ association on +Dungeon+ and the +dungeon+ association on +Trap+ are
# the inverse of each other and the inverse of the +dungeon+ association on +EvilWizard+
# the inverse of each other and the inverse of the +dungeon+ association on +EvilWizard+
# is the +evil_wizard+ association on +Dungeon+ (and vice-versa).
By default,
# is the +evil_wizard+ association on +Dungeon+ (and vice-versa). By default,
# Active Record doesn't know anything about these inverse relationships and so no object
# Active Record doesn't know anything about these inverse relationships and so no object
# loading optimisation is possible.
For example:
# loading optimisation is possible. For example:
#
#
# d = Dungeon.first
# d = Dungeon.first
# t = d.traps.first
# t = d.traps.first
...
@@ -875,8 +875,8 @@ def association_instance_set(name, association)
...
@@ -875,8 +875,8 @@ def association_instance_set(name, association)
#
#
# The +Dungeon+ instances +d+ and <tt>t.dungeon</tt> in the above example refer to
# The +Dungeon+ instances +d+ and <tt>t.dungeon</tt> in the above example refer to
# the same object data from the database, but are actually different in-memory copies
# the same object data from the database, but are actually different in-memory copies
# of that data.
Specifying the <tt>:inverse_of</tt> option on associations lets you tell
# of that data. Specifying the <tt>:inverse_of</tt> option on associations lets you tell
# Active Record about inverse relationships and it will optimise object loading.
For
# Active Record about inverse relationships and it will optimise object loading. For
# example, if we changed our model definitions to:
# example, if we changed our model definitions to:
#
#
# class Dungeon < ActiveRecord::Base
# class Dungeon < ActiveRecord::Base
...
@@ -1059,7 +1059,7 @@ module ClassMethods
...
@@ -1059,7 +1059,7 @@ module ClassMethods
# specify it with this option.
# specify it with this option.
# [:conditions]
# [:conditions]
# Specify the conditions that the associated objects must meet in order to be included as a +WHERE+
# Specify the conditions that the associated objects must meet in order to be included as a +WHERE+
# SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>.
Record creations from
# SQL fragment, such as <tt>price > 5 AND name LIKE 'B%'</tt>. Record creations from
# the association are scoped if a hash is used.
# the association are scoped if a hash is used.
# <tt>has_many :posts, :conditions => {:published => true}</tt> will create published
# <tt>has_many :posts, :conditions => {:published => true}</tt> will create published
# posts with <tt>@blog.posts.create</tt> or <tt>@blog.posts.build</tt>.
# posts with <tt>@blog.posts.create</tt> or <tt>@blog.posts.build</tt>.
...
@@ -1074,8 +1074,8 @@ module ClassMethods
...
@@ -1074,8 +1074,8 @@ module ClassMethods
# Specify the method that returns the primary key used for the association. By default this is +id+.
# Specify the method that returns the primary key used for the association. By default this is +id+.
# [:dependent]
# [:dependent]
# If set to <tt>:destroy</tt> all the associated objects are destroyed
# If set to <tt>:destroy</tt> all the associated objects are destroyed
# alongside this object by calling their +destroy+ method.
If set to <tt>:delete_all</tt> all associated
# alongside this object by calling their +destroy+ method. If set to <tt>:delete_all</tt> all associated
# objects are deleted *without* calling their +destroy+ method.
If set to <tt>:nullify</tt> all associated
# objects are deleted *without* calling their +destroy+ method. If set to <tt>:nullify</tt> all associated
# objects' foreign keys are set to +NULL+ *without* calling their +save+ callbacks. If set to
# objects' foreign keys are set to +NULL+ *without* calling their +save+ callbacks. If set to
# <tt>:restrict</tt> this object cannot be deleted if it has any associated object.
# <tt>:restrict</tt> this object cannot be deleted if it has any associated object.
#
#
...
@@ -1242,7 +1242,7 @@ def has_many(name, options = {}, &extension)
...
@@ -1242,7 +1242,7 @@ def has_many(name, options = {}, &extension)
# you want to do a join but not include the joined columns. Do not forget to include the
# you want to do a join but not include the joined columns. Do not forget to include the
# primary and foreign keys, otherwise it will raise an error.
# primary and foreign keys, otherwise it will raise an error.
# [:through]
# [:through]
# Specifies a Join Model through which to perform the query.
Options for <tt>:class_name</tt>,
# Specifies a Join Model through which to perform the query. Options for <tt>:class_name</tt>,
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
# <tt>:primary_key</tt>, and <tt>:foreign_key</tt> are ignored, as the association uses the
# source reflection. You can only use a <tt>:through</tt> query through a <tt>has_one</tt>
# source reflection. You can only use a <tt>:through</tt> query through a <tt>has_one</tt>
# or <tt>belongs_to</tt> association on the join model.
# or <tt>belongs_to</tt> association on the join model.
...
@@ -1264,7 +1264,7 @@ def has_many(name, options = {}, &extension)
...
@@ -1264,7 +1264,7 @@ def has_many(name, options = {}, &extension)
# By default, only save the associated object if it's a new record.
# By default, only save the associated object if it's a new record.
# [:inverse_of]
# [:inverse_of]
# Specifies the name of the <tt>belongs_to</tt> association on the associated object
# Specifies the name of the <tt>belongs_to</tt> association on the associated object
# that is the inverse of this <tt>has_one</tt> association.
Does not work in combination
# that is the inverse of this <tt>has_one</tt> association. Does not work in combination
# with <tt>:through</tt> or <tt>:as</tt> options.
# with <tt>:through</tt> or <tt>:as</tt> options.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
#
#
...
@@ -1382,7 +1382,7 @@ def has_one(name, options = {})
...
@@ -1382,7 +1382,7 @@ def has_one(name, options = {})
# will be updated with the current time in addition to the updated_at/on attribute.
# will be updated with the current time in addition to the updated_at/on attribute.
# [:inverse_of]
# [:inverse_of]
# Specifies the name of the <tt>has_one</tt> or <tt>has_many</tt> association on the associated
# Specifies the name of the <tt>has_one</tt> or <tt>has_many</tt> association on the associated
# object that is the inverse of this <tt>belongs_to</tt> association.
Does not work in
# object that is the inverse of this <tt>belongs_to</tt> association. Does not work in
# combination with the <tt>:polymorphic</tt> options.
# combination with the <tt>:polymorphic</tt> options.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
# See ActiveRecord::Associations::ClassMethods's overview on Bi-directional associations for more detail.
#
#
...
@@ -1402,15 +1402,15 @@ def belongs_to(name, options = {})
...
@@ -1402,15 +1402,15 @@ def belongs_to(name, options = {})
end
end
# Specifies a many-to-many relationship with another class. This associates two classes via an
# Specifies a many-to-many relationship with another class. This associates two classes via an
# intermediate join table.
Unless the join table is explicitly specified as an option, it is
# intermediate join table. Unless the join table is explicitly specified as an option, it is
# guessed using the lexical order of the class names. So a join between Developer and Project
# guessed using the lexical order of the class names. So a join between Developer and Project
# will give the default join table name of "developers_projects" because "D" outranks "P".
# will give the default join table name of "developers_projects" because "D" outranks "P".
# Note that this precedence is calculated using the <tt><</tt> operator for String.
This
# Note that this precedence is calculated using the <tt><</tt> operator for String. This
# means that if the strings are of different lengths, and the strings are equal when compared
# means that if the strings are of different lengths, and the strings are equal when compared
# up to the shortest length, then the longer string is considered of higher
# up to the shortest length, then the longer string is considered of higher
# lexical precedence than the shorter one.
For example, one would expect the tables "paper_boxes" and "papers"
# lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers"
# to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes",
# to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes",
# but it in fact generates a join table name of "paper_boxes_papers".
Be aware of this caveat, and use the
# but it in fact generates a join table name of "paper_boxes_papers". Be aware of this caveat, and use the
# custom <tt>:join_table</tt> option if you need to.
# custom <tt>:join_table</tt> option if you need to.
#
#
# The join table should not have a primary key or a model associated with it. You must manually generate the
# The join table should not have a primary key or a model associated with it. You must manually generate the
...
@@ -1512,7 +1512,7 @@ def belongs_to(name, options = {})
...
@@ -1512,7 +1512,7 @@ def belongs_to(name, options = {})
# the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
# the association will use "project_id" as the default <tt>:association_foreign_key</tt>.
# [:conditions]
# [:conditions]
# Specify the conditions that the associated object must meet in order to be included as a +WHERE+
# Specify the conditions that the associated object must meet in order to be included as a +WHERE+
# SQL fragment, such as <tt>authorized = 1</tt>.
Record creations from the association are
# SQL fragment, such as <tt>authorized = 1</tt>. Record creations from the association are
# scoped if a hash is used.
# scoped if a hash is used.
# <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
# <tt>has_many :posts, :conditions => {:published => true}</tt> will create published posts with <tt>@blog.posts.create</tt>
# or <tt>@blog.posts.build</tt>.
# or <tt>@blog.posts.build</tt>.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录