Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
91e28aae
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,发现更多精彩内容 >>
提交
91e28aae
编写于
12月 29, 2009
作者:
P
Pratik Naik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Model.having and Relation#having
上级
e8ca22d1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
25 addition
and
18 deletion
+25
-18
activerecord/CHANGELOG
activerecord/CHANGELOG
+4
-0
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations.rb
+4
-2
activerecord/lib/active_record/associations/association_collection.rb
.../lib/active_record/associations/association_collection.rb
+1
-1
activerecord/lib/active_record/base.rb
activerecord/lib/active_record/base.rb
+3
-14
activerecord/lib/active_record/calculations.rb
activerecord/lib/active_record/calculations.rb
+1
-1
activerecord/lib/active_record/relation.rb
activerecord/lib/active_record/relation.rb
+12
-0
未找到文件。
activerecord/CHANGELOG
浏览文件 @
91e28aae
*Edge*
* Add Model.having and Relation#having. [Pratik Naik]
Developer.group("salary").having("sum(salary) > 10000").select("salary")
* Add Relation#count. [Pratik Naik]
legends = People.where("age > 100")
...
...
activerecord/lib/active_record/associations.rb
浏览文件 @
91e28aae
...
...
@@ -1715,7 +1715,8 @@ def construct_finder_arel_with_included_associations(options, join_dependency)
relation
=
relation
.
joins
(
construct_join
(
options
[
:joins
],
scope
)).
select
(
column_aliases
(
join_dependency
)).
group
(
construct_group
(
options
[
:group
],
options
[
:having
],
scope
)).
group
(
options
[
:group
]
||
(
scope
&&
scope
[
:group
])).
having
(
options
[
:having
]
||
(
scope
&&
scope
[
:having
])).
order
(
construct_order
(
options
[
:order
],
scope
)).
where
(
construct_conditions
(
options
[
:conditions
],
scope
)).
from
((
scope
&&
scope
[
:from
])
||
options
[
:from
])
...
...
@@ -1759,7 +1760,8 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
relation
=
relation
.
joins
(
construct_join
(
options
[
:joins
],
scope
)).
where
(
construct_conditions
(
options
[
:conditions
],
scope
)).
group
(
construct_group
(
options
[
:group
],
options
[
:having
],
scope
)).
group
(
options
[
:group
]
||
(
scope
&&
scope
[
:group
])).
having
(
options
[
:having
]
||
(
scope
&&
scope
[
:having
])).
order
(
construct_order
(
options
[
:order
],
scope
)).
limit
(
construct_limit
(
options
[
:limit
],
scope
)).
offset
(
construct_limit
(
options
[
:offset
],
scope
)).
...
...
activerecord/lib/active_record/associations/association_collection.rb
浏览文件 @
91e28aae
...
...
@@ -21,7 +21,7 @@ def initialize(owner, reflection)
construct_sql
end
delegate
:group
,
:order
,
:limit
,
:joins
,
:where
,
:preload
,
:eager_load
,
:from
,
:lock
,
:readonly
,
:to
=>
:scoped
delegate
:group
,
:order
,
:limit
,
:joins
,
:where
,
:preload
,
:eager_load
,
:from
,
:lock
,
:readonly
,
:
having
,
:
to
=>
:scoped
def
select
(
select
=
nil
,
&
block
)
if
block_given?
...
...
activerecord/lib/active_record/base.rb
浏览文件 @
91e28aae
...
...
@@ -652,7 +652,7 @@ def find(*args)
end
end
delegate
:select
,
:group
,
:order
,
:limit
,
:joins
,
:where
,
:preload
,
:eager_load
,
:from
,
:lock
,
:readonly
,
:to
=>
:scoped
delegate
:select
,
:group
,
:order
,
:limit
,
:joins
,
:where
,
:preload
,
:eager_load
,
:from
,
:lock
,
:readonly
,
:
having
,
:
to
=>
:scoped
# A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass in all the
# same arguments to this method as you can to <tt>find(:first)</tt>.
...
...
@@ -1566,7 +1566,8 @@ def construct_finder_arel(options = {}, scope = scope(:find))
joins
(
construct_join
(
options
[
:joins
],
scope
)).
where
(
construct_conditions
(
options
[
:conditions
],
scope
)).
select
(
options
[
:select
]
||
(
scope
&&
scope
[
:select
])
||
default_select
(
options
[
:joins
]
||
(
scope
&&
scope
[
:joins
]))).
group
(
construct_group
(
options
[
:group
],
options
[
:having
],
scope
)).
group
(
options
[
:group
]
||
(
scope
&&
scope
[
:group
])).
having
(
options
[
:having
]
||
(
scope
&&
scope
[
:having
])).
order
(
construct_order
(
options
[
:order
],
scope
)).
limit
(
construct_limit
(
options
[
:limit
],
scope
)).
offset
(
construct_offset
(
options
[
:offset
],
scope
)).
...
...
@@ -1611,18 +1612,6 @@ def construct_join(joins, scope)
end
end
def
construct_group
(
group
,
having
,
scope
)
sql
=
''
if
group
sql
<<
group
.
to_s
sql
<<
" HAVING
#{
sanitize_sql_for_conditions
(
having
)
}
"
if
having
elsif
scope
&&
(
scoped_group
=
scope
[
:group
])
sql
<<
scoped_group
.
to_s
sql
<<
" HAVING
#{
sanitize_sql_for_conditions
(
scope
[
:having
])
}
"
if
scope
[
:having
]
end
sql
end
def
construct_order
(
order
,
scope
)
orders
=
[]
...
...
activerecord/lib/active_record/calculations.rb
浏览文件 @
91e28aae
...
...
@@ -194,7 +194,7 @@ def execute_grouped_calculation(operation, column_name, options, relation) #:nod
options
[
:select
]
<<
",
#{
group_field
}
AS
#{
group_alias
}
"
relation
=
relation
.
select
(
options
[
:select
]).
group
(
construct_group
(
options
[
:group
],
options
[
:having
],
nil
)
)
relation
=
relation
.
select
(
options
[
:select
]).
group
(
options
[
:group
]).
having
(
options
[
:having
]
)
calculated_data
=
connection
.
select_all
(
relation
.
to_sql
)
...
...
activerecord/lib/active_record/relation.rb
浏览文件 @
91e28aae
...
...
@@ -55,6 +55,18 @@ def from(from)
from
.
present?
?
create_new_relation
(
@relation
.
from
(
from
))
:
create_new_relation
end
def
having
(
*
args
)
return
create_new_relation
if
args
.
blank?
if
[
String
,
Hash
,
Array
].
include?
(
args
.
first
.
class
)
havings
=
@klass
.
send
(
:merge_conditions
,
args
.
size
>
1
?
Array
.
wrap
(
args
)
:
args
.
first
)
else
havings
=
args
.
first
end
create_new_relation
(
@relation
.
having
(
havings
))
end
def
group
(
groups
)
groups
.
present?
?
create_new_relation
(
@relation
.
group
(
groups
))
:
create_new_relation
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录