Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
59921676
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,发现更多精彩内容 >>
提交
59921676
编写于
6月 08, 2013
作者:
J
Jeff Cohen
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of github.com:rails/docrails
上级
ad2dc5f9
763e5a86
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
1 deletion
+48
-1
activerecord/lib/active_record/relation/finder_methods.rb
activerecord/lib/active_record/relation/finder_methods.rb
+48
-1
未找到文件。
activerecord/lib/active_record/relation/finder_methods.rb
浏览文件 @
59921676
...
...
@@ -11,7 +11,9 @@ module FinderMethods
# Person.find([1]) # returns an array for the object with ID = 1
# Person.where("administrator = 1").order("created_on DESC").find(1)
#
# Note that returned records may not be in the same order as the ids you
# NOTE: An RecordNotFound will be raised if one or more ids are not returned.
#
# NOTE: that returned records may not be in the same order as the ids you
# provide since database rows are unordered. Give an explicit <tt>order</tt>
# to ensure the results are sorted.
#
...
...
@@ -28,6 +30,38 @@ module FinderMethods
# person.visits += 1
# person.save!
# end
#
# ==== Variations of +find+
#
# Person.where(name: 'Spartacus', rating: 4)
# # returns a chainable list (which can be empty)
#
# Person.find_by(name: 'Spartacus', rating: 4)
# # returns the first item or nil
#
# Person.where(name: 'Spartacus', rating: 4).first_or_initialize
# # returns the first item or returns a new instance (requires you call .save to persist against the database)
#
# Person.where(name: 'Spartacus', rating: 4).first_or_create
# # returns the first item or creates it and returns it, available since rails 3.2.1
#
#
# ==== Alternatives for +find+
#
# Person.where(name: 'Spartacus', rating: 4).exists?(conditions = :none)
# # returns true or false
#
# Person.where(name: 'Spartacus', rating: 4).select("field1, field2, field3")
# # returns a chainable list of instances with only the mentioned fields
#
# Person.where(name: 'Spartacus', rating: 4).ids
# # returns an Array of ids, available since rails 3.2.1
#
# Person.where(name: 'Spartacus', rating: 4).pluck(:field1, :field2)
# # returns an Array of the required fields, available since rails 3.1
#
# Person.arel_table
# # returns an instance of <tt>Arel::Table</tt>, which allows a comprehensive variety of filters
def
find
(
*
args
)
if
block_given?
to_a
.
find
{
|*
block_args
|
yield
(
*
block_args
)
}
...
...
@@ -79,6 +113,19 @@ def take!
# Person.where(["user_name = :u", { u: user_name }]).first
# Person.order("created_on DESC").offset(5).first
# Person.first(3) # returns the first three objects fetched by SELECT * FROM people LIMIT 3
#
# ==== Rails 3
#
# Person.first # SELECT "people".* FROM "people" LIMIT 1
#
# NOTE: Rails 3 may not +order+ this query by be the primary key.
# The order will depend on the database implementation.
# In order to ensure that behavior use <tt>User.order(:id).first</tt> instead.
#
# ==== Rails 4
#
# Person.first # SELECT "people".* FROM "people" ORDER BY "people"."id" ASC LIMIT 1
#
def
first
(
limit
=
nil
)
if
limit
if
order_values
.
empty?
&&
primary_key
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录