Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8bb3b634
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 搜索 >>
提交
8bb3b634
编写于
7月 20, 2010
作者:
N
Neeraj Singh
提交者:
José Valim
7月 21, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Timestamp columns of HABTM join table should record timestamps
[#5161 state:resolved]
上级
01add55d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
57 addition
and
5 deletion
+57
-5
activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
...ecord/associations/has_and_belongs_to_many_association.rb
+5
-1
activerecord/lib/active_record/timestamp.rb
activerecord/lib/active_record/timestamp.rb
+16
-3
activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
...associations/has_and_belongs_to_many_associations_test.rb
+34
-1
activerecord/test/schema/schema.rb
activerecord/test/schema/schema.rb
+2
-0
未找到文件。
activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
浏览文件 @
8bb3b634
...
...
@@ -53,7 +53,11 @@ def insert_record(record, force = true, validate = true)
when
@reflection
.
association_foreign_key
.
to_s
attrs
[
relation
[
column
.
name
]]
=
record
.
id
else
if
record
.
has_attribute?
(
column
.
name
)
if
record
.
send
(
:all_timestamp_attributes
).
include?
(
column
.
name
.
to_sym
)
if
record
.
record_timestamps
attrs
[
relation
[
column
.
name
]]
=
record
.
send
(
:current_time_from_proper_timezone
)
end
elsif
record
.
has_attribute?
(
column
.
name
)
value
=
@owner
.
send
(
:quote_value
,
record
[
column
.
name
],
column
)
attrs
[
relation
[
column
.
name
]]
=
value
unless
value
.
nil?
end
...
...
activerecord/lib/active_record/timestamp.rb
浏览文件 @
8bb3b634
...
...
@@ -39,8 +39,9 @@ def create #:nodoc:
if
record_timestamps
current_time
=
current_time_from_proper_timezone
write_attribute
(
'created_at'
,
current_time
)
if
respond_to?
(
:created_at
)
&&
created_at
.
nil?
write_attribute
(
'created_on'
,
current_time
)
if
respond_to?
(
:created_on
)
&&
created_on
.
nil?
timestamp_attributes_for_create
.
each
do
|
column
|
write_attribute
(
column
.
to_s
,
current_time
)
if
respond_to?
(
column
)
&&
self
.
send
(
column
).
nil?
end
timestamp_attributes_for_update_in_model
.
each
do
|
column
|
write_attribute
(
column
.
to_s
,
current_time
)
if
self
.
send
(
column
).
nil?
...
...
@@ -65,7 +66,19 @@ def record_update_timestamps #:nodoc:
end
def
timestamp_attributes_for_update_in_model
#:nodoc:
[
:updated_at
,
:updated_on
].
select
{
|
elem
|
respond_to?
(
elem
)
}
timestamp_attributes_for_update
.
select
{
|
elem
|
respond_to?
(
elem
)
}
end
def
timestamp_attributes_for_update
#:nodoc:
[
:updated_at
,
:updated_on
]
end
def
timestamp_attributes_for_create
#:nodoc:
[
:created_at
,
:created_on
]
end
def
all_timestamp_attributes
#:nodoc:
timestamp_attributes_for_update
+
timestamp_attributes_for_create
end
def
current_time_from_proper_timezone
#:nodoc:
...
...
activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
浏览文件 @
8bb3b634
...
...
@@ -85,7 +85,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
fixtures
:accounts
,
:companies
,
:categories
,
:posts
,
:categories_posts
,
:developers
,
:projects
,
:developers_projects
,
:parrots
,
:pirates
,
:treasures
,
:price_estimates
,
:tags
,
:taggings
def
test_should_property_quote_string_primary_keys
def
setup_data_for_habtm_case
ActiveRecord
::
Base
.
connection
.
execute
(
'delete from countries_treaties'
)
country
=
Country
.
new
(
:name
=>
'India'
)
country
.
country_id
=
'c1'
country
.
save!
...
...
@@ -93,6 +95,10 @@ def test_should_property_quote_string_primary_keys
treaty
=
Treaty
.
new
(
:name
=>
'peace'
)
treaty
.
treaty_id
=
't1'
country
.
treaties
<<
treaty
end
def
test_should_property_quote_string_primary_keys
setup_data_for_habtm_case
con
=
ActiveRecord
::
Base
.
connection
sql
=
'select * from countries_treaties'
...
...
@@ -101,6 +107,33 @@ def test_should_property_quote_string_primary_keys
assert_equal
't1'
,
record
[
1
]
end
def
test_should_record_timestamp_for_join_table
setup_data_for_habtm_case
con
=
ActiveRecord
::
Base
.
connection
sql
=
'select * from countries_treaties'
record
=
con
.
select_rows
(
sql
).
last
assert_not_nil
record
[
2
]
assert_not_nil
record
[
3
]
assert_match
%r{
\d
{4}-
\d
{2}-
\d
{2}
\d
{2}:
\d
{2}:
\d
{2}}
,
record
[
2
]
assert_match
%r{
\d
{4}-
\d
{2}-
\d
{2}
\d
{2}:
\d
{2}:
\d
{2}}
,
record
[
3
]
end
def
test_should_record_timestamp_for_join_table_only_if_timestamp_should_be_recorded
begin
Treaty
.
record_timestamps
=
false
setup_data_for_habtm_case
con
=
ActiveRecord
::
Base
.
connection
sql
=
'select * from countries_treaties'
record
=
con
.
select_rows
(
sql
).
last
assert_nil
record
[
2
]
assert_nil
record
[
3
]
ensure
Treaty
.
record_timestamps
=
true
end
end
def
test_has_and_belongs_to_many
david
=
Developer
.
find
(
1
)
...
...
activerecord/test/schema/schema.rb
浏览文件 @
8bb3b634
...
...
@@ -611,6 +611,8 @@ def create_table(*args, &block)
create_table
:countries_treaties
,
:force
=>
true
,
:id
=>
false
do
|
t
|
t
.
string
:country_id
,
:null
=>
false
t
.
string
:treaty_id
,
:null
=>
false
t
.
datetime
:created_at
t
.
datetime
:updated_at
end
except
'SQLite'
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录