ROR_find查询

ror里的查找主要使用find命令:
先贴一段api
 
 
[b]find[/b](*args)
<div class="description">find operates with four different retrieval approaches:
[list][*]find by [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001839]id[/url] - this can either be a specific[url=http://api.rubyonrails.org/classes/activerecord/base.html#m001839]id[/url] (1), a list of ids (1, 5, 6), or anarray of ids ([5, 6, 10]). if no record can be found for [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001780]all[/url] of the listed ids, then [url=http://api.rubyonrails.org/classes/activerecord/recordnotfound.html]recordnotfound[/url] will be raised.
[*]find [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001778]first[/url] - this will return the [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001778]first[/url] record matched by the options used.these options can either be specific conditions or merely an order. if norecord can be matched, <tt>nil</tt> is returned. use <tt>model.find(:[url=http://api.rubyonrails.org/classes/activerecord/base.html#m001778]first[/url], *args)</tt> or its shortcut<tt>model.first(*args)</tt>.
[*]find [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001779]last[/url] - this will return the [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001779]last[/url] record matched by the options used. theseoptions can either be specific conditions or merely an order. if no recordcan be matched, <tt>nil</tt> is returned. use <tt>model.find(:[url=http://api.rubyonrails.org/classes/activerecord/base.html#m001779]last[/url], *args)</tt> or its shortcut<tt>model.last(*args)</tt>.
[*]find [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001780]all[/url] - this will return [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001780]all[/url] the records matched by the options used.if no records are found, an empty array is returned. use <tt>model.find(:[url=http://api.rubyonrails.org/classes/activerecord/base.html#m001780]all[/url], *args)</tt> or its shortcut<tt>model.all(*args)</tt>.
[/list]all approaches accept an options [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001872]hash[/url] astheir [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001779]last[/url] parameter.
<h4>parameters</h4>[list][*]<tt>:conditions</tt> - an sql fragment like "administrator = 1",<tt>[ "user_name = ?", username ]</tt>, or <tt>["user_name =:user_name", { :user_name => user_name }]</tt>. see conditions inthe intro.
[*]<tt>:order</tt> - an sql fragment like "created_at desc, name".
[*]<tt>:group</tt> - an attribute name by which the result should be grouped.uses the <tt>group by</tt> sql-clause.
[*]<tt>:having</tt> - combined with +:group+ this can be used to filter therecords that a <tt>group by</tt> returns. uses the <tt>having</tt>sql-clause.
[*]<tt>:limit</tt> - an integer determining the limit on the number of rowsthat should be returned.
[*]<tt>:offset</tt> - an integer determining the offset from where the rowsshould be fetched. so at 5, it would skip rows 0 through 4.
[*]<tt>:joins</tt> - either an sql fragment for additional joins like"left join comments on comments.post_id = [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001839]id[/url]" (rarely needed), named associationsin the same form used for the <tt>:include</tt> option, which will performan <tt>inner join</tt> on the associated table(s), or an array containing amixture of both strings and named associations. if the value is a string,then the records will be returned read-only since they will have [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001863]attributes[/url] that do not correspond to thetable‘s [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001809]columns[/url]. pass <tt>:readonly=> false</tt> to override.
[*]<tt>:include</tt> - names associations that should be loaded alongside. thesymbols named refer to already defined associations. see eager loadingunder associations.
[*]<tt>:select</tt> - by default, this is "*" as in "select *from", but can be changed if you, for example, want to do a join butnot include the joined [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001809]columns[/url]. takes astring with the select sql fragment (e.g. "[url=http://api.rubyonrails.org/classes/activerecord/base.html#m001839]id[/url], name").
[*]<tt>:from</tt> - by default, this is the table name of the class, but canbe changed to an alternate table name (or even the name of a databaseview).
[*]<tt>:readonly</tt> - mark the returned records read-only so they cannot besaved or updated.
[*]<tt>:lock</tt> - an sql fragment like "for update" or "lockin share mode". <tt>:lock => true</tt> gives [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001879]connection[/url]‘s default exclusive lock,usually "for update".
[/list]<h4>examples</h4><pre> # 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")</pre>note that returned records may not be in the same order as the ids youprovide since database rows are unordered. give an explicit <tt>:order</tt>to ensure the results are sorted.
<h4>examples</h4><pre> # 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")</pre>example for [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001777]find[/url] with a lock: imagine twoconcurrent transactions: each will read <tt>person.visits == 2</tt>, add 1to it, and [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001844]save[/url], resulting in two saves of<tt>person.visits = 3</tt>. by locking the row, the second transaction hasto wait until the [url=http://api.rubyonrails.org/classes/activerecord/base.html#m001778]first[/url] is finished; we getthe expected <tt>person.visits == 4</tt>.
<pre> person.transaction do person = person.find(1, :lock => true) person.visits += 1 person.save! end</pre>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值