Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2b63a354
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 搜索 >>
提交
2b63a354
编写于
2月 20, 2015
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge branch 'rm-take' into 4-1-stable
上级
2dff9d0f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
43 addition
and
1 deletion
+43
-1
activerecord/lib/active_record/associations/collection_association.rb
.../lib/active_record/associations/collection_association.rb
+10
-0
activerecord/lib/active_record/associations/collection_proxy.rb
...record/lib/active_record/associations/collection_proxy.rb
+4
-0
activerecord/test/cases/associations/has_many_associations_test.rb
...ord/test/cases/associations/has_many_associations_test.rb
+29
-1
未找到文件。
activerecord/lib/active_record/associations/collection_association.rb
浏览文件 @
2b63a354
...
...
@@ -129,6 +129,16 @@ def last(*args)
first_nth_or_last
(
:last
,
*
args
)
end
def
take
if
loaded?
target
.
first
else
scope
.
take
.
tap
do
|
record
|
set_inverse_instance
record
if
record
.
is_a?
ActiveRecord
::
Base
end
end
end
def
build
(
attributes
=
{},
&
block
)
if
attributes
.
is_a?
(
Array
)
attributes
.
collect
{
|
attr
|
build
(
attr
,
&
block
)
}
...
...
activerecord/lib/active_record/associations/collection_proxy.rb
浏览文件 @
2b63a354
...
...
@@ -227,6 +227,10 @@ def last(*args)
@association
.
last
(
*
args
)
end
def
take
@association
.
take
end
# Returns a new object of the collection type that has been instantiated
# with +attributes+ and linked to this object, but have not yet been saved.
# You can pass an array of attributes hashes, this will return an array
...
...
activerecord/test/cases/associations/has_many_associations_test.rb
浏览文件 @
2b63a354
...
...
@@ -33,6 +33,8 @@
require
'models/tyre'
require
'models/subscriber'
require
'models/subscription'
require
'models/zine'
require
'models/interest'
class
HasManyAssociationsTestForReorderWithJoinDependency
<
ActiveRecord
::
TestCase
fixtures
:authors
,
:posts
,
:comments
...
...
@@ -97,7 +99,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
fixtures
:accounts
,
:categories
,
:companies
,
:developers
,
:projects
,
:developers_projects
,
:topics
,
:authors
,
:comments
,
:posts
,
:readers
,
:taggings
,
:cars
,
:jobs
,
:tags
,
:categorizations
:categorizations
,
:zines
,
:interests
def
setup
Client
.
destroyed_client_ids
.
clear
...
...
@@ -435,6 +437,32 @@ def test_dynamic_find_should_respect_association_order
assert_equal
companies
(
:another_first_firm_client
),
companies
(
:first_firm
).
clients_sorted_desc
.
find_by_type
(
'Client'
)
end
def
test_taking
posts
(
:other_by_bob
).
destroy
assert_equal
posts
(
:misc_by_bob
),
authors
(
:bob
).
posts
.
take
assert_equal
posts
(
:misc_by_bob
),
authors
(
:bob
).
posts
.
take!
authors
(
:bob
).
posts
.
to_a
assert_equal
posts
(
:misc_by_bob
),
authors
(
:bob
).
posts
.
take
assert_equal
posts
(
:misc_by_bob
),
authors
(
:bob
).
posts
.
take!
end
def
test_taking_not_found
authors
(
:bob
).
posts
.
delete_all
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
authors
(
:bob
).
posts
.
take!
}
authors
(
:bob
).
posts
.
to_a
assert_raise
(
ActiveRecord
::
RecordNotFound
)
{
authors
(
:bob
).
posts
.
take!
}
end
def
test_taking_with_inverse_of
interests
(
:woodsmanship
).
destroy
interests
(
:survival
).
destroy
zine
=
zines
(
:going_out
)
interest
=
zine
.
interests
.
take
assert_equal
interests
(
:hunting
),
interest
assert_same
zine
,
interest
.
zine
end
def
test_cant_save_has_many_readonly_association
authors
(
:david
).
readonly_comments
.
each
{
|
c
|
assert_raise
(
ActiveRecord
::
ReadOnlyRecord
)
{
c
.
save!
}
}
authors
(
:david
).
readonly_comments
.
each
{
|
c
|
assert
c
.
readonly?
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录