Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
245542ea
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,发现更多精彩内容 >>
提交
245542ea
编写于
3月 27, 2011
作者:
S
Sebastian Martinez
提交者:
Santiago Pastorino
3月 27, 2011
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added new #update_column method.
Signed-off-by:
N
Santiago Pastorino
<
santiago@wyeworks.com
>
上级
e6a8a3ad
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
132 addition
and
21 deletion
+132
-21
activerecord/CHANGELOG
activerecord/CHANGELOG
+9
-0
activerecord/lib/active_record/attribute_methods/write.rb
activerecord/lib/active_record/attribute_methods/write.rb
+1
-0
activerecord/lib/active_record/persistence.rb
activerecord/lib/active_record/persistence.rb
+14
-0
activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
...associations/has_and_belongs_to_many_associations_test.rb
+1
-1
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+3
-3
activerecord/test/cases/associations/has_many_through_associations_test.rb
.../cases/associations/has_many_through_associations_test.rb
+2
-2
activerecord/test/cases/associations/has_one_through_associations_test.rb
...t/cases/associations/has_one_through_associations_test.rb
+2
-2
activerecord/test/cases/associations/join_model_test.rb
activerecord/test/cases/associations/join_model_test.rb
+6
-6
activerecord/test/cases/associations_test.rb
activerecord/test/cases/associations_test.rb
+2
-2
activerecord/test/cases/base_test.rb
activerecord/test/cases/base_test.rb
+1
-1
activerecord/test/cases/calculations_test.rb
activerecord/test/cases/calculations_test.rb
+2
-2
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/dirty_test.rb
+1
-1
activerecord/test/cases/persistence_test.rb
activerecord/test/cases/persistence_test.rb
+86
-0
activerecord/test/cases/timestamp_test.rb
activerecord/test/cases/timestamp_test.rb
+2
-1
未找到文件。
activerecord/CHANGELOG
浏览文件 @
245542ea
*Rails 3.1.0 (unreleased)*
*Rails 3.1.0 (unreleased)*
* Added an update_column method on ActiveRecord. This new method updates a given attribute on an object, skipping validations and callbacks.
It is recommended to use #update_attribute unless you are sure you do not want to execute any callback, including the modification of
the updated_at column. It should not be called on new records.
Example:
User.first.update_column(:name, "sebastian") # => true
[Sebastian Martinez]
* Associations with a :through option can now use *any* association as the
* Associations with a :through option can now use *any* association as the
through or source association, including other associations which have a
through or source association, including other associations which have a
:through option and has_and_belongs_to_many associations
:through option and has_and_belongs_to_many associations
...
...
activerecord/lib/active_record/attribute_methods/write.rb
浏览文件 @
245542ea
...
@@ -32,6 +32,7 @@ def write_attribute(attr_name, value)
...
@@ -32,6 +32,7 @@ def write_attribute(attr_name, value)
@attributes
[
attr_name
]
=
value
@attributes
[
attr_name
]
=
value
end
end
end
end
alias_method
:raw_write_attribute
,
:write_attribute
private
private
# Handle *= for method_missing.
# Handle *= for method_missing.
...
...
activerecord/lib/active_record/persistence.rb
浏览文件 @
245542ea
...
@@ -119,6 +119,20 @@ def update_attribute(name, value)
...
@@ -119,6 +119,20 @@ def update_attribute(name, value)
save
(
:validate
=>
false
)
save
(
:validate
=>
false
)
end
end
# Updates a single attribute of an object, without calling save.
#
# * Validation is skipped.
# * Callbacks are skipped.
# * updated_at/updated_on column is not updated if that column is available.
#
def
update_column
(
name
,
value
)
name
=
name
.
to_s
raise
ActiveRecordError
,
"
#{
name
}
is marked as readonly"
if
self
.
class
.
readonly_attributes
.
include?
(
name
)
raise
ActiveRecordError
,
"can not update on a new record object"
unless
persisted?
raw_write_attribute
(
name
,
value
)
self
.
class
.
update_all
({
name
=>
value
},
self
.
class
.
primary_key
=>
id
)
==
1
end
# Updates the attributes of the model from the passed-in hash and saves the
# Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving
# record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned.
# will fail and false will be returned.
...
...
activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
浏览文件 @
245542ea
...
@@ -604,7 +604,7 @@ def test_update_attributes_after_push_without_duplicate_join_table_rows
...
@@ -604,7 +604,7 @@ def test_update_attributes_after_push_without_duplicate_join_table_rows
project
=
SpecialProject
.
create
(
"name"
=>
"Special Project"
)
project
=
SpecialProject
.
create
(
"name"
=>
"Special Project"
)
assert
developer
.
save
assert
developer
.
save
developer
.
projects
<<
project
developer
.
projects
<<
project
developer
.
update_
attribute
(
"name"
,
"Bruza"
)
developer
.
update_
column
(
"name"
,
"Bruza"
)
assert_equal
1
,
Developer
.
connection
.
select_value
(
<<-
end_sql
).
to_i
assert_equal
1
,
Developer
.
connection
.
select_value
(
<<-
end_sql
).
to_i
SELECT count(*) FROM developers_projects
SELECT count(*) FROM developers_projects
WHERE project_id =
#{
project
.
id
}
WHERE project_id =
#{
project
.
id
}
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
245542ea
...
@@ -639,7 +639,7 @@ def test_deleting_updates_counter_cache_without_dependent_option
...
@@ -639,7 +639,7 @@ def test_deleting_updates_counter_cache_without_dependent_option
def
test_deleting_updates_counter_cache_with_dependent_delete_all
def
test_deleting_updates_counter_cache_with_dependent_delete_all
post
=
posts
(
:welcome
)
post
=
posts
(
:welcome
)
post
.
update_
attribute
(
:taggings_with_delete_all_count
,
post
.
taggings_count
)
post
.
update_
column
(
:taggings_with_delete_all_count
,
post
.
taggings_count
)
assert_difference
"post.reload.taggings_with_delete_all_count"
,
-
1
do
assert_difference
"post.reload.taggings_with_delete_all_count"
,
-
1
do
post
.
taggings_with_delete_all
.
delete
(
post
.
taggings_with_delete_all
.
first
)
post
.
taggings_with_delete_all
.
delete
(
post
.
taggings_with_delete_all
.
first
)
...
@@ -648,7 +648,7 @@ def test_deleting_updates_counter_cache_with_dependent_delete_all
...
@@ -648,7 +648,7 @@ def test_deleting_updates_counter_cache_with_dependent_delete_all
def
test_deleting_updates_counter_cache_with_dependent_destroy
def
test_deleting_updates_counter_cache_with_dependent_destroy
post
=
posts
(
:welcome
)
post
=
posts
(
:welcome
)
post
.
update_
attribute
(
:taggings_with_destroy_count
,
post
.
taggings_count
)
post
.
update_
column
(
:taggings_with_destroy_count
,
post
.
taggings_count
)
assert_difference
"post.reload.taggings_with_destroy_count"
,
-
1
do
assert_difference
"post.reload.taggings_with_destroy_count"
,
-
1
do
post
.
taggings_with_destroy
.
delete
(
post
.
taggings_with_destroy
.
first
)
post
.
taggings_with_destroy
.
delete
(
post
.
taggings_with_destroy
.
first
)
...
@@ -787,7 +787,7 @@ def test_delete_all_association_with_primary_key_deletes_correct_records
...
@@ -787,7 +787,7 @@ def test_delete_all_association_with_primary_key_deletes_correct_records
firm
=
Firm
.
find
(
:first
)
firm
=
Firm
.
find
(
:first
)
# break the vanilla firm_id foreign key
# break the vanilla firm_id foreign key
assert_equal
2
,
firm
.
clients
.
count
assert_equal
2
,
firm
.
clients
.
count
firm
.
clients
.
first
.
update_
attribute
(
:firm_id
,
nil
)
firm
.
clients
.
first
.
update_
column
(
:firm_id
,
nil
)
assert_equal
1
,
firm
.
clients
(
true
).
count
assert_equal
1
,
firm
.
clients
(
true
).
count
assert_equal
1
,
firm
.
clients_using_primary_key_with_delete_all
.
count
assert_equal
1
,
firm
.
clients_using_primary_key_with_delete_all
.
count
old_record
=
firm
.
clients_using_primary_key_with_delete_all
.
first
old_record
=
firm
.
clients_using_primary_key_with_delete_all
.
first
...
...
activerecord/test/cases/associations/has_many_through_associations_test.rb
浏览文件 @
245542ea
...
@@ -286,7 +286,7 @@ def test_update_counter_caches_on_delete
...
@@ -286,7 +286,7 @@ def test_update_counter_caches_on_delete
def
test_update_counter_caches_on_delete_with_dependent_destroy
def
test_update_counter_caches_on_delete_with_dependent_destroy
post
=
posts
(
:welcome
)
post
=
posts
(
:welcome
)
tag
=
post
.
tags
.
create!
(
:name
=>
'doomed'
)
tag
=
post
.
tags
.
create!
(
:name
=>
'doomed'
)
post
.
update_
attribute
(
:tags_with_destroy_count
,
post
.
tags
.
count
)
post
.
update_
column
(
:tags_with_destroy_count
,
post
.
tags
.
count
)
assert_difference
[
'post.reload.taggings_count'
,
'post.reload.tags_with_destroy_count'
],
-
1
do
assert_difference
[
'post.reload.taggings_count'
,
'post.reload.tags_with_destroy_count'
],
-
1
do
posts
(
:welcome
).
tags_with_destroy
.
delete
(
tag
)
posts
(
:welcome
).
tags_with_destroy
.
delete
(
tag
)
...
@@ -296,7 +296,7 @@ def test_update_counter_caches_on_delete_with_dependent_destroy
...
@@ -296,7 +296,7 @@ def test_update_counter_caches_on_delete_with_dependent_destroy
def
test_update_counter_caches_on_delete_with_dependent_nullify
def
test_update_counter_caches_on_delete_with_dependent_nullify
post
=
posts
(
:welcome
)
post
=
posts
(
:welcome
)
tag
=
post
.
tags
.
create!
(
:name
=>
'doomed'
)
tag
=
post
.
tags
.
create!
(
:name
=>
'doomed'
)
post
.
update_
attribute
(
:tags_with_nullify_count
,
post
.
tags
.
count
)
post
.
update_
column
(
:tags_with_nullify_count
,
post
.
tags
.
count
)
assert_no_difference
'post.reload.taggings_count'
do
assert_no_difference
'post.reload.taggings_count'
do
assert_difference
'post.reload.tags_with_nullify_count'
,
-
1
do
assert_difference
'post.reload.tags_with_nullify_count'
,
-
1
do
...
...
activerecord/test/cases/associations/has_one_through_associations_test.rb
浏览文件 @
245542ea
...
@@ -90,12 +90,12 @@ def test_has_one_through_eager_loading_through_polymorphic
...
@@ -90,12 +90,12 @@ def test_has_one_through_eager_loading_through_polymorphic
def
test_has_one_through_with_conditions_eager_loading
def
test_has_one_through_with_conditions_eager_loading
# conditions on the through table
# conditions on the through table
assert_equal
clubs
(
:moustache_club
),
Member
.
find
(
@member
.
id
,
:include
=>
:favourite_club
).
favourite_club
assert_equal
clubs
(
:moustache_club
),
Member
.
find
(
@member
.
id
,
:include
=>
:favourite_club
).
favourite_club
memberships
(
:membership_of_favourite_club
).
update_
attribute
(
:favourite
,
false
)
memberships
(
:membership_of_favourite_club
).
update_
column
(
:favourite
,
false
)
assert_equal
nil
,
Member
.
find
(
@member
.
id
,
:include
=>
:favourite_club
).
reload
.
favourite_club
assert_equal
nil
,
Member
.
find
(
@member
.
id
,
:include
=>
:favourite_club
).
reload
.
favourite_club
# conditions on the source table
# conditions on the source table
assert_equal
clubs
(
:moustache_club
),
Member
.
find
(
@member
.
id
,
:include
=>
:hairy_club
).
hairy_club
assert_equal
clubs
(
:moustache_club
),
Member
.
find
(
@member
.
id
,
:include
=>
:hairy_club
).
hairy_club
clubs
(
:moustache_club
).
update_
attribute
(
:name
,
"Association of Clean-Shaven Persons"
)
clubs
(
:moustache_club
).
update_
column
(
:name
,
"Association of Clean-Shaven Persons"
)
assert_equal
nil
,
Member
.
find
(
@member
.
id
,
:include
=>
:hairy_club
).
reload
.
hairy_club
assert_equal
nil
,
Member
.
find
(
@member
.
id
,
:include
=>
:hairy_club
).
reload
.
hairy_club
end
end
...
...
activerecord/test/cases/associations/join_model_test.rb
浏览文件 @
245542ea
...
@@ -161,7 +161,7 @@ def test_create_polymorphic_has_one_with_scope
...
@@ -161,7 +161,7 @@ def test_create_polymorphic_has_one_with_scope
def
test_delete_polymorphic_has_many_with_delete_all
def
test_delete_polymorphic_has_many_with_delete_all
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
posts
(
:welcome
).
taggings
.
first
.
update_
attribute
:taggable_type
,
'PostWithHasManyDeleteAll'
posts
(
:welcome
).
taggings
.
first
.
update_
column
:taggable_type
,
'PostWithHasManyDeleteAll'
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:delete_all
)
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:delete_all
)
old_count
=
Tagging
.
count
old_count
=
Tagging
.
count
...
@@ -172,7 +172,7 @@ def test_delete_polymorphic_has_many_with_delete_all
...
@@ -172,7 +172,7 @@ def test_delete_polymorphic_has_many_with_delete_all
def
test_delete_polymorphic_has_many_with_destroy
def
test_delete_polymorphic_has_many_with_destroy
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
posts
(
:welcome
).
taggings
.
first
.
update_
attribute
:taggable_type
,
'PostWithHasManyDestroy'
posts
(
:welcome
).
taggings
.
first
.
update_
column
:taggable_type
,
'PostWithHasManyDestroy'
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:destroy
)
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:destroy
)
old_count
=
Tagging
.
count
old_count
=
Tagging
.
count
...
@@ -183,7 +183,7 @@ def test_delete_polymorphic_has_many_with_destroy
...
@@ -183,7 +183,7 @@ def test_delete_polymorphic_has_many_with_destroy
def
test_delete_polymorphic_has_many_with_nullify
def
test_delete_polymorphic_has_many_with_nullify
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
assert_equal
1
,
posts
(
:welcome
).
taggings
.
count
posts
(
:welcome
).
taggings
.
first
.
update_
attribute
:taggable_type
,
'PostWithHasManyNullify'
posts
(
:welcome
).
taggings
.
first
.
update_
column
:taggable_type
,
'PostWithHasManyNullify'
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:nullify
)
post
=
find_post_with_dependency
(
1
,
:has_many
,
:taggings
,
:nullify
)
old_count
=
Tagging
.
count
old_count
=
Tagging
.
count
...
@@ -194,7 +194,7 @@ def test_delete_polymorphic_has_many_with_nullify
...
@@ -194,7 +194,7 @@ def test_delete_polymorphic_has_many_with_nullify
def
test_delete_polymorphic_has_one_with_destroy
def
test_delete_polymorphic_has_one_with_destroy
assert
posts
(
:welcome
).
tagging
assert
posts
(
:welcome
).
tagging
posts
(
:welcome
).
tagging
.
update_
attribute
:taggable_type
,
'PostWithHasOneDestroy'
posts
(
:welcome
).
tagging
.
update_
column
:taggable_type
,
'PostWithHasOneDestroy'
post
=
find_post_with_dependency
(
1
,
:has_one
,
:tagging
,
:destroy
)
post
=
find_post_with_dependency
(
1
,
:has_one
,
:tagging
,
:destroy
)
old_count
=
Tagging
.
count
old_count
=
Tagging
.
count
...
@@ -205,7 +205,7 @@ def test_delete_polymorphic_has_one_with_destroy
...
@@ -205,7 +205,7 @@ def test_delete_polymorphic_has_one_with_destroy
def
test_delete_polymorphic_has_one_with_nullify
def
test_delete_polymorphic_has_one_with_nullify
assert
posts
(
:welcome
).
tagging
assert
posts
(
:welcome
).
tagging
posts
(
:welcome
).
tagging
.
update_
attribute
:taggable_type
,
'PostWithHasOneNullify'
posts
(
:welcome
).
tagging
.
update_
column
:taggable_type
,
'PostWithHasOneNullify'
post
=
find_post_with_dependency
(
1
,
:has_one
,
:tagging
,
:nullify
)
post
=
find_post_with_dependency
(
1
,
:has_one
,
:tagging
,
:nullify
)
old_count
=
Tagging
.
count
old_count
=
Tagging
.
count
...
@@ -707,7 +707,7 @@ def test_has_many_through_goes_through_all_sti_classes
...
@@ -707,7 +707,7 @@ def test_has_many_through_goes_through_all_sti_classes
# create dynamic Post models to allow different dependency options
# create dynamic Post models to allow different dependency options
def
find_post_with_dependency
(
post_id
,
association
,
association_name
,
dependency
)
def
find_post_with_dependency
(
post_id
,
association
,
association_name
,
dependency
)
class_name
=
"PostWith
#{
association
.
to_s
.
classify
}#{
dependency
.
to_s
.
classify
}
"
class_name
=
"PostWith
#{
association
.
to_s
.
classify
}#{
dependency
.
to_s
.
classify
}
"
Post
.
find
(
post_id
).
update_
attribute
:type
,
class_name
Post
.
find
(
post_id
).
update_
column
:type
,
class_name
klass
=
Object
.
const_set
(
class_name
,
Class
.
new
(
ActiveRecord
::
Base
))
klass
=
Object
.
const_set
(
class_name
,
Class
.
new
(
ActiveRecord
::
Base
))
klass
.
set_table_name
'posts'
klass
.
set_table_name
'posts'
klass
.
send
(
association
,
association_name
,
:as
=>
:taggable
,
:dependent
=>
dependency
)
klass
.
send
(
association
,
association_name
,
:as
=>
:taggable
,
:dependent
=>
dependency
)
...
...
activerecord/test/cases/associations_test.rb
浏览文件 @
245542ea
...
@@ -66,7 +66,7 @@ def test_loading_the_association_target_should_load_most_recent_attributes_for_c
...
@@ -66,7 +66,7 @@ def test_loading_the_association_target_should_load_most_recent_attributes_for_c
ship
=
Ship
.
create!
(
:name
=>
"The good ship Dollypop"
)
ship
=
Ship
.
create!
(
:name
=>
"The good ship Dollypop"
)
part
=
ship
.
parts
.
create!
(
:name
=>
"Mast"
)
part
=
ship
.
parts
.
create!
(
:name
=>
"Mast"
)
part
.
mark_for_destruction
part
.
mark_for_destruction
ShipPart
.
find
(
part
.
id
).
update_
attribute
(
:name
,
'Deck'
)
ShipPart
.
find
(
part
.
id
).
update_
column
(
:name
,
'Deck'
)
ship
.
parts
.
send
(
:load_target
)
ship
.
parts
.
send
(
:load_target
)
assert_equal
'Deck'
,
ship
.
parts
[
0
].
name
assert_equal
'Deck'
,
ship
.
parts
[
0
].
name
end
end
...
@@ -170,7 +170,7 @@ def test_save_on_parent_does_not_load_target
...
@@ -170,7 +170,7 @@ def test_save_on_parent_does_not_load_target
david
=
developers
(
:david
)
david
=
developers
(
:david
)
assert
!
david
.
projects
.
loaded?
assert
!
david
.
projects
.
loaded?
david
.
update_
attribute
(
:created_at
,
Time
.
now
)
david
.
update_
column
(
:created_at
,
Time
.
now
)
assert
!
david
.
projects
.
loaded?
assert
!
david
.
projects
.
loaded?
end
end
...
...
activerecord/test/cases/base_test.rb
浏览文件 @
245542ea
...
@@ -484,7 +484,7 @@ def test_non_valid_identifier_column_name
...
@@ -484,7 +484,7 @@ def test_non_valid_identifier_column_name
weird
.
reload
weird
.
reload
assert_equal
'value'
,
weird
.
send
(
'a$b'
)
assert_equal
'value'
,
weird
.
send
(
'a$b'
)
weird
.
update_
attribute
(
'a$b'
,
'value2'
)
weird
.
update_
column
(
'a$b'
,
'value2'
)
weird
.
reload
weird
.
reload
assert_equal
'value2'
,
weird
.
send
(
'a$b'
)
assert_equal
'value2'
,
weird
.
send
(
'a$b'
)
end
end
...
...
activerecord/test/cases/calculations_test.rb
浏览文件 @
245542ea
...
@@ -311,8 +311,8 @@ def test_should_count_scoped_select
...
@@ -311,8 +311,8 @@ def test_should_count_scoped_select
def
test_should_count_scoped_select_with_options
def
test_should_count_scoped_select_with_options
Account
.
update_all
(
"credit_limit = NULL"
)
Account
.
update_all
(
"credit_limit = NULL"
)
Account
.
last
.
update_
attribute
(
'credit_limit'
,
49
)
Account
.
last
.
update_
column
(
'credit_limit'
,
49
)
Account
.
first
.
update_
attribute
(
'credit_limit'
,
51
)
Account
.
first
.
update_
column
(
'credit_limit'
,
51
)
assert_equal
1
,
Account
.
scoped
(
:select
=>
"credit_limit"
).
count
(
:conditions
=>
[
'credit_limit >= 50'
])
assert_equal
1
,
Account
.
scoped
(
:select
=>
"credit_limit"
).
count
(
:conditions
=>
[
'credit_limit >= 50'
])
end
end
...
...
activerecord/test/cases/dirty_test.rb
浏览文件 @
245542ea
...
@@ -413,7 +413,7 @@ def test_save_should_not_save_serialized_attribute_with_partial_updates_if_not_p
...
@@ -413,7 +413,7 @@ def test_save_should_not_save_serialized_attribute_with_partial_updates_if_not_p
with_partial_updates
(
Topic
)
do
with_partial_updates
(
Topic
)
do
Topic
.
create!
(
:author_name
=>
'Bill'
,
:content
=>
{
:a
=>
"a"
})
Topic
.
create!
(
:author_name
=>
'Bill'
,
:content
=>
{
:a
=>
"a"
})
topic
=
Topic
.
select
(
'id, author_name'
).
first
topic
=
Topic
.
select
(
'id, author_name'
).
first
topic
.
update_
attribute
:author_name
,
'John'
topic
.
update_
column
:author_name
,
'John'
topic
=
Topic
.
first
topic
=
Topic
.
first
assert_not_nil
topic
.
content
assert_not_nil
topic
.
content
end
end
...
...
activerecord/test/cases/persistence_test.rb
浏览文件 @
245542ea
...
@@ -389,6 +389,92 @@ def test_update_attribute_for_updated_at_on
...
@@ -389,6 +389,92 @@ def test_update_attribute_for_updated_at_on
assert_not_equal
prev_month
,
developer
.
updated_at
assert_not_equal
prev_month
,
developer
.
updated_at
end
end
def
test_update_column
topic
=
Topic
.
find
(
1
)
topic
.
update_column
(
"approved"
,
true
)
assert
topic
.
approved?
topic
.
reload
assert
topic
.
approved?
topic
.
update_column
(
:approved
,
false
)
assert
!
topic
.
approved?
topic
.
reload
assert
!
topic
.
approved?
end
def
test_update_column_should_not_use_setter_method
dev
=
Developer
.
find
(
1
)
dev
.
instance_eval
{
def
salary
=
(
value
);
write_attribute
(
:salary
,
value
*
2
);
end
}
dev
.
update_column
(
:salary
,
80000
)
assert_equal
80000
,
dev
.
salary
dev
.
reload
assert_equal
80000
,
dev
.
salary
end
def
test_update_column_should_raise_exception_if_new_record
topic
=
Topic
.
new
assert_raises
(
ActiveRecord
::
ActiveRecordError
)
{
topic
.
update_column
(
"approved"
,
false
)
}
end
def
test_update_column_should_not_leave_the_object_dirty
topic
=
Topic
.
find
(
1
)
topic
.
update_attribute
(
"content"
,
"Have a nice day"
)
topic
.
reload
topic
.
update_column
(
:content
,
"You too"
)
assert_equal
[],
topic
.
changed
topic
.
reload
topic
.
update_column
(
"content"
,
"Have a nice day"
)
assert_equal
[],
topic
.
changed
end
def
test_update_column_with_model_having_primary_key_other_than_id
minivan
=
Minivan
.
find
(
'm1'
)
new_name
=
'sebavan'
minivan
.
update_column
(
:name
,
new_name
)
assert_equal
new_name
,
minivan
.
name
end
def
test_update_column_for_readonly_attribute
minivan
=
Minivan
.
find
(
'm1'
)
prev_color
=
minivan
.
color
assert_raises
(
ActiveRecord
::
ActiveRecordError
)
{
minivan
.
update_column
(
:color
,
'black'
)
}
assert_equal
prev_color
,
minivan
.
color
end
def
test_update_column_should_not_modify_updated_at
developer
=
Developer
.
find
(
1
)
prev_month
=
Time
.
now
.
prev_month
developer
.
update_column
(
:updated_at
,
prev_month
)
assert_equal
prev_month
,
developer
.
updated_at
developer
.
update_column
(
:salary
,
80001
)
assert_equal
prev_month
,
developer
.
updated_at
developer
.
reload
assert_equal
prev_month
,
developer
.
updated_at
end
def
test_update_column_with_one_changed_and_one_updated
t
=
Topic
.
order
(
'id'
).
limit
(
1
).
first
title
,
author_name
=
t
.
title
,
t
.
author_name
t
.
author_name
=
'John'
t
.
update_column
(
:title
,
'super_title'
)
assert_equal
'John'
,
t
.
author_name
assert_equal
'super_title'
,
t
.
title
assert
t
.
changed?
,
"topic should have changed"
assert
t
.
author_name_changed?
,
"author_name should have changed"
t
.
reload
assert_equal
author_name
,
t
.
author_name
assert_equal
'super_title'
,
t
.
title
end
def
test_update_attributes
def
test_update_attributes
topic
=
Topic
.
find
(
1
)
topic
=
Topic
.
find
(
1
)
assert
!
topic
.
approved?
assert
!
topic
.
approved?
...
...
activerecord/test/cases/timestamp_test.rb
浏览文件 @
245542ea
...
@@ -131,8 +131,9 @@ def test_touching_a_record_touches_parent_record_and_grandparent_record
...
@@ -131,8 +131,9 @@ def test_touching_a_record_touches_parent_record_and_grandparent_record
toy
=
Toy
.
first
toy
=
Toy
.
first
pet
=
toy
.
pet
pet
=
toy
.
pet
owner
=
pet
.
owner
owner
=
pet
.
owner
time
=
3
.
days
.
ago
owner
.
update_
attribute
(
:updated_at
,
(
time
=
3
.
days
.
ago
)
)
owner
.
update_
column
(
:updated_at
,
time
)
toy
.
touch
toy
.
touch
owner
.
reload
owner
.
reload
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录