Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3c580182
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,体验更适合开发者的 AI 搜索 >>
提交
3c580182
编写于
11月 08, 2012
作者:
A
AvnerCohen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1.9 hash syntax changes
上级
c04abbaa
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
111 addition
and
111 deletion
+111
-111
activerecord/lib/active_record/aggregations.rb
activerecord/lib/active_record/aggregations.rb
+18
-18
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+53
-53
activerecord/lib/active_record/autosave_association.rb
activerecord/lib/active_record/autosave_association.rb
+11
-11
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+19
-19
activerecord/lib/active_record/callbacks.rb
activerecord/lib/active_record/callbacks.rb
+1
-1
activerecord/lib/active_record/connection_handling.rb
activerecord/lib/active_record/connection_handling.rb
+8
-8
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+1
-1
未找到文件。
activerecord/lib/active_record/aggregations.rb
浏览文件 @
3c580182
...
...
@@ -16,8 +16,8 @@ def clear_aggregation_cache #:nodoc:
# the database).
#
# class Customer < ActiveRecord::Base
# composed_of :balance,
:class_name => "Money", :mapping =>
%w(balance amount)
# composed_of :address,
:mapping =>
[ %w(address_street street), %w(address_city city) ]
# composed_of :balance,
class_name: "Money", mapping:
%w(balance amount)
# composed_of :address,
mapping:
[ %w(address_street street), %w(address_city city) ]
# end
#
# The customer class now has the following methods to manipulate the value objects:
...
...
@@ -138,15 +138,15 @@ def clear_aggregation_cache #:nodoc:
#
# class NetworkResource < ActiveRecord::Base
# composed_of :cidr,
#
:class_name =>
'NetAddr::CIDR',
#
:mapping =>
[ %w(network_address network), %w(cidr_range bits) ],
#
:allow_nil =>
true,
#
:constructor =>
Proc.new { |network_address, cidr_range| NetAddr::CIDR.create("#{network_address}/#{cidr_range}") },
#
:converter =>
Proc.new { |value| NetAddr::CIDR.create(value.is_a?(Array) ? value.join('/') : value) }
#
class_name:
'NetAddr::CIDR',
#
mapping:
[ %w(network_address network), %w(cidr_range bits) ],
#
allow_nil:
true,
#
constructor:
Proc.new { |network_address, cidr_range| NetAddr::CIDR.create("#{network_address}/#{cidr_range}") },
#
converter:
Proc.new { |value| NetAddr::CIDR.create(value.is_a?(Array) ? value.join('/') : value) }
# end
#
# # This calls the :constructor
# network_resource = NetworkResource.new(
:network_address => '192.168.0.1', :cidr_range =>
24)
# network_resource = NetworkResource.new(
network_address: '192.168.0.1', cidr_range:
24)
#
# # These assignments will both use the :converter
# network_resource.cidr = [ '192.168.2.1', 8 ]
...
...
@@ -165,7 +165,7 @@ def clear_aggregation_cache #:nodoc:
# by specifying an instance of the value object in the conditions hash. The following example
# finds all customers with +balance_amount+ equal to 20 and +balance_currency+ equal to "USD":
#
# Customer.where(
:balance =>
Money.new(20, "USD")).all
# Customer.where(
balance:
Money.new(20, "USD")).all
#
module
ClassMethods
# Adds reader and writer methods for manipulating a value object:
...
...
@@ -197,17 +197,17 @@ module ClassMethods
# can return nil to skip the assignment.
#
# Option examples:
# composed_of :temperature,
:mapping =>
%w(reading celsius)
# composed_of :balance,
:class_name => "Money", :mapping =>
%w(balance amount),
#
:converter =>
Proc.new { |balance| balance.to_money }
# composed_of :address,
:mapping =>
[ %w(address_street street), %w(address_city city) ]
# composed_of :temperature,
mapping:
%w(reading celsius)
# composed_of :balance,
class_name: "Money", mapping:
%w(balance amount),
#
converter:
Proc.new { |balance| balance.to_money }
# composed_of :address,
mapping:
[ %w(address_street street), %w(address_city city) ]
# composed_of :gps_location
# composed_of :gps_location,
:allow_nil =>
true
# composed_of :gps_location,
allow_nil:
true
# composed_of :ip_address,
#
:class_name =>
'IPAddr',
#
:mapping =>
%w(ip to_i),
#
:constructor =>
Proc.new { |ip| IPAddr.new(ip, Socket::AF_INET) },
#
:converter =>
Proc.new { |ip| ip.is_a?(Integer) ? IPAddr.new(ip, Socket::AF_INET) : IPAddr.new(ip.to_s) }
#
class_name:
'IPAddr',
#
mapping:
%w(ip to_i),
#
constructor:
Proc.new { |ip| IPAddr.new(ip, Socket::AF_INET) },
#
converter:
Proc.new { |ip| ip.is_a?(Integer) ? IPAddr.new(ip, Socket::AF_INET) : IPAddr.new(ip.to_s) }
#
def
composed_of
(
part_id
,
options
=
{})
options
.
assert_valid_keys
(
:class_name
,
:mapping
,
:allow_nil
,
:constructor
,
:converter
)
...
...
activerecord/lib/active_record/associations.rb
浏览文件 @
3c580182
...
...
@@ -305,11 +305,11 @@ def association_instance_set(name, association)
# 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
...
...
@@ -426,7 +426,7 @@ def association_instance_set(name, association)
# 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)
# ...
...
...
@@ -437,7 +437,7 @@ def association_instance_set(name, association)
#
# 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+.
...
...
@@ -507,7 +507,7 @@ def association_instance_set(name, association)
#
# class Author < ActiveRecord::Base
# has_many :authorships
# has_many :books,
:through =>
:authorships
# has_many :books,
through:
:authorships
# end
#
# class Authorship < ActiveRecord::Base
...
...
@@ -523,7 +523,7 @@ def association_instance_set(name, association)
#
# class Firm < ActiveRecord::Base
# has_many :clients
# has_many :invoices,
:through =>
:clients
# has_many :invoices,
through:
:clients
# end
#
# class Client < ActiveRecord::Base
...
...
@@ -543,7 +543,7 @@ def association_instance_set(name, association)
#
# class Group < ActiveRecord::Base
# has_many :users
# has_many :avatars,
:through =>
:users
# has_many :avatars,
through:
:users
# end
#
# class User < ActiveRecord::Base
...
...
@@ -571,7 +571,7 @@ def association_instance_set(name, association)
# 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
...
...
@@ -579,7 +579,7 @@ def association_instance_set(name, association)
#
# class Taggable < ActiveRecord::Base
# belongs_to :post
# belongs_to :tag,
:inverse_of =>
:taggings
# belongs_to :tag,
inverse_of:
:taggings
# end
#
# == Nested Associations
...
...
@@ -589,8 +589,8 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -608,12 +608,12 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -632,11 +632,11 @@ def association_instance_set(name, association)
# 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
...
...
@@ -653,7 +653,7 @@ def association_instance_set(name, association)
# 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)
...
...
@@ -661,8 +661,8 @@ def association_instance_set(name, association)
# 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
...
...
@@ -724,7 +724,7 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -749,13 +749,13 @@ def association_instance_set(name, association)
# 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)
...
...
@@ -767,7 +767,7 @@ def association_instance_set(name, association)
# 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.
...
...
@@ -775,7 +775,7 @@ def association_instance_set(name, association)
# 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
...
...
@@ -809,10 +809,10 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -821,10 +821,10 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -868,7 +868,7 @@ def association_instance_set(name, association)
#
# 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
...
...
@@ -910,16 +910,16 @@ def association_instance_set(name, association)
# 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
...
...
@@ -942,7 +942,7 @@ def association_instance_set(name, association)
# 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
#
...
...
@@ -1026,12 +1026,12 @@ module ClassMethods
# 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.
...
...
@@ -1049,8 +1049,8 @@ module ClassMethods
# 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?]
...
...
@@ -1078,7 +1078,7 @@ module ClassMethods
# === 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>
...
...
@@ -1088,8 +1088,8 @@ module ClassMethods
# * <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.
...
...
@@ -1143,7 +1143,7 @@ module ClassMethods
# [: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
...
...
@@ -1207,7 +1207,7 @@ def has_many(name, scope = nil, options = {}, &extension)
# === 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>)
...
...
@@ -1247,7 +1247,7 @@ def has_many(name, scope = nil, options = {}, &extension)
# [: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
...
...
@@ -1267,11 +1267,11 @@ def has_many(name, scope = nil, options = {}, &extension)
# 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
...
...
@@ -1326,12 +1326,12 @@ def has_one(name, scope = nil, options = {})
# 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.
...
...
@@ -1351,7 +1351,7 @@ def has_one(name, scope = nil, options = {})
# <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]
...
...
@@ -1409,7 +1409,7 @@ def belongs_to(name, scope = nil, options = {})
#
# 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
...
...
activerecord/lib/active_record/autosave_association.rb
浏览文件 @
3c580182
...
...
@@ -16,7 +16,7 @@ module ActiveRecord
# Note that it also means that associations marked for destruction won't
# be destroyed directly. They will however still be marked for destruction.
#
# Note that <tt>
:autosave =>
false</tt> is not same as not declaring <tt>:autosave</tt>.
# Note that <tt>
autosave:
false</tt> is not same as not declaring <tt>:autosave</tt>.
# When the <tt>:autosave</tt> option is not present new associations are saved.
#
# == Validation
...
...
@@ -37,7 +37,7 @@ module ActiveRecord
# === One-to-one Example
#
# class Post
# has_one :author,
:autosave =>
true
# has_one :author,
autosave:
true
# end
#
# Saving changes to the parent and its associated model can now be performed
...
...
@@ -81,27 +81,27 @@ module ActiveRecord
# has_many :comments # :autosave option is not declared
# end
#
# post = Post.new(
:title =>
'ruby rocks')
# post.comments.build(
:body =>
'hello world')
# post = Post.new(
title:
'ruby rocks')
# post.comments.build(
body:
'hello world')
# post.save # => saves both post and comment
#
# post = Post.create(
:title =>
'ruby rocks')
# post.comments.build(
:body =>
'hello world')
# post = Post.create(
title:
'ruby rocks')
# post.comments.build(
body:
'hello world')
# post.save # => saves both post and comment
#
# post = Post.create(
:title =>
'ruby rocks')
# post.comments.create(
:body =>
'hello world')
# post = Post.create(
title:
'ruby rocks')
# post.comments.create(
body:
'hello world')
# post.save # => saves both post and comment
#
# When <tt>:autosave</tt> is true all children are saved, no matter whether they
# are new records or not:
#
# class Post
# has_many :comments,
:autosave =>
true
# has_many :comments,
autosave:
true
# end
#
# post = Post.create(
:title =>
'ruby rocks')
# post.comments.create(
:body =>
'hello world')
# post = Post.create(
title:
'ruby rocks')
# post.comments.create(
body:
'hello world')
# post.comments[0].body = 'hi everyone'
# post.save # => saves both post and comment, with 'hi everyone' as body
#
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
3c580182
...
...
@@ -36,7 +36,7 @@ module ActiveRecord #:nodoc:
# method is especially useful when you're receiving the data from somewhere else, like an
# HTTP request. It works like this:
#
# user = User.new(
:name => "David", :occupation =>
"Code Artist")
# user = User.new(
name: "David", occupation:
"Code Artist")
# user.name # => "David"
#
# You can also use block initialization:
...
...
@@ -69,7 +69,7 @@ module ActiveRecord #:nodoc:
# end
#
# def self.authenticate_safely_simply(user_name, password)
# where(
:user_name => user_name, :password =>
password).first
# where(
user_name: user_name, password:
password).first
# end
# end
#
...
...
@@ -87,27 +87,27 @@ module ActiveRecord #:nodoc:
#
# Company.where(
# "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
# {
:id => 3, :name => "37signals", :division => "First", :accounting_date =>
'2005-01-01' }
# {
id: 3, name: "37signals", division: "First", accounting_date:
'2005-01-01' }
# ).first
#
# Similarly, a simple hash without a statement will generate conditions based on equality with the SQL AND
# operator. For instance:
#
# Student.where(
:first_name => "Harvey", :status =>
1)
# Student.where(
first_name: "Harvey", status:
1)
# Student.where(params[:student])
#
# A range may be used in the hash to use the SQL BETWEEN operator:
#
# Student.where(
:grade =>
9..12)
# Student.where(
grade:
9..12)
#
# An array may be used in the hash to use the SQL IN operator:
#
# Student.where(
:grade =>
[9,11,12])
# Student.where(
grade:
[9,11,12])
#
# When joining tables, nested hashes or keys written in the form 'table_name.column_name'
# can be used to qualify the table name of a particular condition. For instance:
#
# Student.joins(:schools).where(
:schools => { :category =>
'public' })
# Student.joins(:schools).where(
schools: { category:
'public' })
# Student.joins(:schools).where('schools.category' => 'public' )
#
# == Overwriting default accessors
...
...
@@ -141,10 +141,10 @@ module ActiveRecord #:nodoc:
# For example, an Active Record User with the <tt>name</tt> attribute has a <tt>name?</tt> method that you can call
# to determine whether the user has a name:
#
# user = User.new(
:name =>
"David")
# user = User.new(
name:
"David")
# user.name? # => true
#
# anonymous = User.new(
:name =>
"")
# anonymous = User.new(
name:
"")
# anonymous.name? # => false
#
# == Accessing attributes before they have been typecasted
...
...
@@ -165,8 +165,8 @@ module ActiveRecord #:nodoc:
# to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt> and thus produces finders
# like <tt>Person.find_by_user_name</tt>, <tt>Person.find_all_by_last_name</tt>, and
# <tt>Payment.find_by_transaction_id</tt>. Instead of writing
# <tt>Person.where(
:user_name =>
user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
# And instead of writing <tt>Person.where(
:last_name =>
last_name).all</tt>, you just do
# <tt>Person.where(
user_name:
user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
# And instead of writing <tt>Person.where(
last_name:
last_name).all</tt>, you just do
# <tt>Person.find_all_by_last_name(last_name)</tt>.
#
# It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
...
...
@@ -175,7 +175,7 @@ module ActiveRecord #:nodoc:
#
# It's also possible to use multiple attributes in the same find by separating them with "_and_".
#
# Person.where(
:user_name => user_name, :password =>
password).first
# Person.where(
user_name: user_name, password:
password).first
# Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
#
# It's even possible to call these dynamic finder methods on relations and named scopes.
...
...
@@ -189,13 +189,13 @@ module ActiveRecord #:nodoc:
# unless they are given in a block.
#
# # No 'Summer' tag exists
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(
:name =>
"Summer")
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(
name:
"Summer")
#
# # Now the 'Summer' tag does exist
# Tag.find_or_create_by_name("Summer") # equal to Tag.find_by_name("Summer")
#
# # Now 'Bob' exist and is an 'admin'
# User.find_or_create_by_name('Bob',
:age =>
40) { |u| u.admin = true }
# User.find_or_create_by_name('Bob',
age:
40) { |u| u.admin = true }
#
# Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will
# raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid.
...
...
@@ -210,7 +210,7 @@ module ActiveRecord #:nodoc:
# To find by a subset of the attributes to be used for instantiating a new object, pass a hash instead of
# a list of parameters.
#
# Tag.find_or_create_by_name(
:name => "rails", :creator =>
current_user)
# Tag.find_or_create_by_name(
name: "rails", creator:
current_user)
#
# That will either find an existing tag named "rails", or create a new one while setting the
# user that created it.
...
...
@@ -232,7 +232,7 @@ module ActiveRecord #:nodoc:
# serialize :preferences
# end
#
# user = User.create(
:preferences =>
{ "background" => "black", "display" => large })
# user = User.create(
preferences:
{ "background" => "black", "display" => large })
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
#
# You can also specify a class option as the second parameter that'll raise an exception
...
...
@@ -242,7 +242,7 @@ module ActiveRecord #:nodoc:
# serialize :preferences, Hash
# end
#
# user = User.create(
:preferences =>
%w( one two three ))
# user = User.create(
preferences:
%w( one two three ))
# User.find(user.id).preferences # raises SerializationTypeMismatch
#
# When you specify a class option, the default value for that attribute will be a new
...
...
@@ -267,9 +267,9 @@ module ActiveRecord #:nodoc:
# class Client < Company; end
# class PriorityClient < Client; end
#
# When you do <tt>Firm.create(
:name =>
"37signals")</tt>, this record will be saved in
# When you do <tt>Firm.create(
name:
"37signals")</tt>, this record will be saved in
# the companies table with type = "Firm". You can then fetch this row again using
# <tt>Company.where(
:name =>
'37signals').first</tt> and it will return a Firm object.
# <tt>Company.where(
name:
'37signals').first</tt> and it will return a Firm object.
#
# If you don't have a type column defined in your table, single-table inheritance won't
# be triggered. In that case, it'll work just like normal subclasses with no special magic
...
...
activerecord/lib/active_record/callbacks.rb
浏览文件 @
3c580182
...
...
@@ -35,7 +35,7 @@ module ActiveRecord
# class CreditCard < ActiveRecord::Base
# # Strip everything but digits, so the user can specify "555 234 34" or
# # "5552-3434" and both will mean "55523434"
# before_validation(
:on =>
:create) do
# before_validation(
on:
:create) do
# self.number = number.gsub(/[^0-9]/, "") if attribute_present?("number")
# end
# end
...
...
activerecord/lib/active_record/connection_handling.rb
浏览文件 @
3c580182
...
...
@@ -5,18 +5,18 @@ module ConnectionHandling
# example for regular databases (MySQL, Postgresql, etc):
#
# ActiveRecord::Base.establish_connection(
#
:adapter =>
"mysql",
#
:host =>
"localhost",
#
:username =>
"myuser",
#
:password =>
"mypass",
#
:database =>
"somedatabase"
#
adapter:
"mysql",
#
host:
"localhost",
#
username:
"myuser",
#
password:
"mypass",
#
database:
"somedatabase"
# )
#
# Example for SQLite database:
#
# ActiveRecord::Base.establish_connection(
#
:adapter =>
"sqlite",
#
:database =>
"path/to/dbfile"
#
adapter:
"sqlite",
#
database:
"path/to/dbfile"
# )
#
# Also accepts keys as strings (for parsing from YAML for example):
...
...
@@ -64,7 +64,7 @@ def connection_id=(connection_id)
# Returns the configuration of the associated connection as a hash:
#
# ActiveRecord::Base.connection_config
# # => {
:pool=>5, :timeout=>5000, :database=>"db/development.sqlite3", :adapter=>
"sqlite3"}
# # => {
pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter:
"sqlite3"}
#
# Please use only for reading.
def
connection_config
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
3c580182
...
...
@@ -157,7 +157,7 @@ def relation #:nodoc:
#
# ==== Example:
# # Instantiates a single new object
# User.new(
:first_name =>
'Jamie')
# User.new(
first_name:
'Jamie')
def
initialize
(
attributes
=
nil
)
defaults
=
self
.
class
.
column_defaults
.
dup
defaults
.
each
{
|
k
,
v
|
defaults
[
k
]
=
v
.
dup
if
v
.
duplicable?
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录