ROR_find查询

    ror里的查找主要使用find命令:
先贴一段api
 
 
find(*args)   
<div class="description">find operates with four different retrieval approaches:
  • find by id - this can either be a specificid (1), a list of ids (1, 5, 6), or anarray 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 norecord can be matched, <tt>nil</tt> is returned. use <tt>model.find(:first, *args)</tt> or its shortcut<tt>model.first(*args)</tt>.
  • find last - this will return the last 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(:last, *args)</tt> or its shortcut<tt>model.last(*args)</tt>.
  • find all - this will return all the records matched by the options used.if no records are found, an empty array is returned. use <tt>model.find(:all, *args)</tt> or its shortcut<tt>model.all(*args)</tt>.
all approaches accept an options hash astheir last parameter.
<h4>parameters</h4>
  • <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 = id" (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 attributes that do not correspond to thetable‘s columns. 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 columns. takes astring with the select sql fragment (e.g. "id, 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 connection‘s default exclusive lock,usually "for update".
<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", rder => "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, rder => "created_on desc", ffset => 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, rder => "created_on desc", ffset => 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, ffset => 10, :limit => 10)  person.find(:all, :include => [ :account, :friends ])  person.find(:all, :group => "category")</pre>example for find with a lock: imagine twoconcurrent transactions: each will read <tt>person.visits == 2</tt>, add 1to it, and save, resulting in two saves of<tt>person.visits = 3</tt>. by locking the row, the second transaction hasto wait until the first 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>

你可能感兴趣的:(java,工作)