Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
593e21d6
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 搜索 >>
提交
593e21d6
编写于
5月 13, 2008
作者:
X
Xavier Noria
提交者:
Jeremy Kemper
5月 12, 2008
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Dirty attributes aren't cleared if save fails. [#174 state:resolved]
Signed-off-by:
N
Jeremy Kemper
<
jeremy@bitsweat.net
>
上级
a425cd14
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
30 addition
and
9 deletion
+30
-9
activerecord/lib/active_record/dirty.rb
activerecord/lib/active_record/dirty.rb
+8
-8
activerecord/test/cases/dirty_test.rb
activerecord/test/cases/dirty_test.rb
+20
-1
activerecord/test/models/pirate.rb
activerecord/test/models/pirate.rb
+2
-0
未找到文件。
activerecord/lib/active_record/dirty.rb
浏览文件 @
593e21d6
...
...
@@ -69,19 +69,19 @@ def changes
changed
.
inject
({})
{
|
h
,
attr
|
h
[
attr
]
=
attribute_change
(
attr
);
h
}
end
# Clear changed attributes after they are saved.
# Attempts to +save+ the record and clears changed attributes if successful.
def
save_with_dirty
(
*
args
)
#:nodoc:
save_without_dirty
(
*
args
)
ensure
changed_attributes
.
clear
if
status
=
save_without_dirty
(
*
args
)
changed_attributes
.
clear
end
status
end
#
Clear changed attributes after they are saved
.
#
Attempts to <tt>save!</tt> the record and clears changed attributes if successful
.
def
save_with_dirty!
(
*
args
)
#:nodoc:
save_without_dirty!
(
*
args
)
ensure
status
=
save_without_dirty!
(
*
args
)
changed_attributes
.
clear
status
end
private
...
...
activerecord/test/cases/dirty_test.rb
浏览文件 @
593e21d6
...
...
@@ -78,7 +78,7 @@ def test_attribute_will_change!
end
def
test_association_assignment_changes_foreign_key
pirate
=
Pirate
.
create!
pirate
=
Pirate
.
create!
(
:catchphrase
=>
'jarl'
)
pirate
.
parrot
=
Parrot
.
create!
assert
pirate
.
changed?
assert_equal
%w(parrot_id)
,
pirate
.
changed
...
...
@@ -115,6 +115,18 @@ def test_partial_update
end
end
def
test_changed_attributes_should_be_preserved_if_save_failure
pirate
=
Pirate
.
new
pirate
.
parrot_id
=
1
assert
!
pirate
.
save
check_pirate_after_save_failure
(
pirate
)
pirate
=
Pirate
.
new
pirate
.
parrot_id
=
1
assert_raises
(
ActiveRecord
::
RecordInvalid
)
{
pirate
.
save!
}
check_pirate_after_save_failure
(
pirate
)
end
private
def
with_partial_updates
(
klass
,
on
=
true
)
old
=
klass
.
partial_updates?
...
...
@@ -123,4 +135,11 @@ def with_partial_updates(klass, on = true)
ensure
klass
.
partial_updates
=
old
end
def
check_pirate_after_save_failure
(
pirate
)
assert
pirate
.
changed?
assert
pirate
.
parrot_id_changed?
assert_equal
%w(parrot_id)
,
pirate
.
changed
assert_nil
pirate
.
parrot_id_was
end
end
activerecord/test/models/pirate.rb
浏览文件 @
593e21d6
...
...
@@ -4,4 +4,6 @@ class Pirate < ActiveRecord::Base
has_many
:treasures
,
:as
=>
:looter
has_many
:treasure_estimates
,
:through
=>
:treasures
,
:source
=>
:price_estimates
validates_presence_of
:catchphrase
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录