ROR里的查找主要使用find命令:
先贴一段API
find
(*args)
Find operates with four different retrieval approaches:
- Find by id
- This can either be a specific
id
(1), a list of ids (1, 5, 6), or an
array of ids ([5, 6, 10]). If no record can be found for all
of the listed ids, then RecordNotFound
will be raised.
- Find first
- This will return the first
record matched by the options used.
These options can either be specific conditions or merely an order. If no
record can be matched, nil
is returned. Use Model.find(:first
, *args)
or its shortcut
Model.first(*args)
.
- Find last
- This will return the last
record matched by the options used. These
options can either be specific conditions or merely an order. If no record
can be matched, nil
is returned. Use Model.find(:last
, *args)
or its shortcut
Model.last(*args)
.
- Find all
- This will return all
the records matched by the options used.
If no records are found, an empty array is returned. Use Model.find(:all
, *args)
or its shortcut
Model.all(*args)
.
All approaches accept an options hash
as
their last
parameter.
Parameters
-
:conditions
- An SQL fragment like "administrator = 1",
[ "user_name = ?", username ]
, or ["user_name =
:user_name", { :user_name => user_name }]
. See conditions in
the intro.
-
:order
- An SQL fragment like "created_at DESC, name".
-
:group
- An attribute name by which the result should be grouped.
Uses the GROUP BY
SQL-clause.
-
:having
- Combined with +:group+ this can be used to filter the
records that a GROUP BY
returns. Uses the HAVING
SQL-clause.
-
:limit
- An integer determining the limit on the number of rows
that should be returned.
-
:offset
- An integer determining the offset from where the rows
should be fetched. So at 5, it would skip rows 0 through 4.
-
:joins
- Either an SQL fragment for additional joins like
"LEFT JOIN comments ON comments.post_id = id
" (rarely needed), named associations
in the same form used for the :include
option, which will perform
an INNER JOIN
on the associated table(s), or an array containing a
mixture of both strings and named associations. If the value is a string,
then the records will be returned read-only since they will have attributes
that do not correspond to the
table‘s columns
. Pass :readonly
=> false
to override.
-
:include
- Names associations that should be loaded alongside. The
symbols named refer to already defined associations. See eager loading
under Associations.
-
:select
- By default, this is "*" as in "SELECT *
FROM", but can be changed if you, for example, want to do a join but
not include the joined columns
. Takes a
string with the SELECT SQL fragment (e.g. "id
, name").
-
:from
- By default, this is the table name of the class, but can
be changed to an alternate table name (or even the name of a database
view).
-
:readonly
- Mark the returned records read-only so they cannot be
saved or updated.
-
:lock
- An SQL fragment like "FOR UPDATE" or "LOCK
IN SHARE MODE". :lock => true
gives connection
‘s default exclusive lock,
usually "FOR UPDATE".
Examples
# find by id
Person.find(1) # returns the object for ID = 1
Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
Person.find([1]) # returns an array for the object with ID = 1
Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC")
Note that returned records may not be in the same order as the ids you
provide since database rows are unordered. Give an explicit :order
to ensure the results are sorted.
Examples
# find first
Person.find(:first) # returns the first object fetched by SELECT * FROM people
Person.find(:first, :conditions => [ "user_name = ?", user_name])
Person.find(:first, :conditions => [ "user_name = :u", { :u => user_name }])
Person.find(:first, :order => "created_on DESC", :offset => 5)
# find last
Person.find(:last) # returns the last object fetched by SELECT * FROM people
Person.find(:last, :conditions => [ "user_name = ?", user_name])
Person.find(:last, :order => "created_on DESC", :offset => 5)
# find all
Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people
Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
Person.find(:all, :conditions => { :friends => ["Bob", "Steve", "Fred"] }
Person.find(:all, :offset => 10, :limit => 10)
Person.find(:all, :include => [ :account, :friends ])
Person.find(:all, :group => "category")
Example for find
with a lock: Imagine two
concurrent transactions: each will read person.visits == 2
, add 1
to it, and save
, resulting in two saves of
person.visits = 3
. By locking the row, the second transaction has
to wait until the first
is finished; we get
the expected person.visits == 4
.
Person.transaction do
person = Person.find(1, :lock => true)
person.visits += 1
person.save!
end
然后介绍另外一些常见的查询:
1.关联查询
@goods = Good.find(:all,
:conditions =>["goods.id=?",params[:id]],
:include => [:user])
http://hideto.iteye.com/blog/105774
还可以查看这个网址
分享到:
相关推荐
ROR_shopping_microservice
ror_invoicing_frontend项目设置yarn install编译和热重装以进行开发yarn serve编译并最小化生产yarn build整理和修复文件yarn lint自定义配置请参阅。
ruby_full:RoR_Full_29 :: Ruby_reports
ROR_lesson:乌迪米
Mominon sosvovävovarore äror fofulollol avov ålolaror. # Degibber a sentence and print the result in English >python Gibberish.py -d "Coc'esostot unone popetotitote fofroromomagoge" "English" It is...
保养指南针 该应用程序是由提供的 gem生成的。 Rails Composer是开源的,并受到订户的支持。 请加入RailsApps以支持Rails Composer的开发。问题? 问题? 需要帮忙? 询问带有标签“ railsapps”的堆栈溢出。...
自述文件该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明...
自述文件该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明...
自述文件 来自Blogger项目(作为Odin项目的一部分完成) 额外的信用现在,我们在博客应用程序中具有以Author类表示的经过身份验证的用户的概念,并且允许作者创建和编辑文章。 如何使文章的所有权更加明确和安全,...
自述文件该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明...
软件工程师测试任务(Ruby On Rails) 目的 我们希望获得用于构建各种应用程序的后端:Web或移动应用程序。 使用Ruby On Rails为学习门户开发API。 任务说明 该应用程序具有用户和选修课。 您可以在应用程序中创建...
安装Ruby 安装无业游民 安装Vagrant AWS插件 安装Windows RSync 安装Git 安装图书管理员厨师宝石 确保正确设置了PATH环境变量...在命令行中,将目录更改为以下之一:lamp_aws,lamp_local,ror_aws,ror_local 运
自述文件 该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明… 如果您...
自述文件 带有TDD的Rails API Udemy的课程带有Ruby on Rails的REST API 您可能要讲的内容: Ruby版本:2.7.0 Rails版本:5.2.4.4 组态 数据库创建 数据库初始化 测试套件:Rspec 服务(作业队列,缓存服务器,...
此示例展示了一种通过 ZMQ 将 Ruby on Rails Web 应用程序与 Julia 连接的方法。 ... 我只是在学习,我希望这篇文章对你有所帮助。 欢迎所有反馈! 基本上,我们在 Julia 中创建了一个 ZMQ 服务器,它将按照网页的...
自述文件 该自述文件通常会记录启动和运行应用程序所需的所有步骤。 您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明… 如果您...
更新版本1.1 即将推出的功能 UI重新设计 图像上传持久性 费用出口 TransactApp-Ruby on Rails Web应用程序 TransactApp允许用户创建一个帐户并登录以对提交的任何交易进行分组 目录 关于该项目 ...
ror实例ror实例ror实例
自述文件这是一个基本的Web应用程序,用于存储在轨道上使用ruby制作的文档。 首次运行: rails server这将构建应用程序,下载所需的gem并启动Puma- Rails的Web服务器。 我们可以创建新用户,然后添加文档。
RoR性能优化经验谈,牛人对ror的优化,值得大家阅读