`
hsyzijvaa
  • 浏览: 111861 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

ROR_find查询

    博客分类:
  • java
阅读更多
    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>
0
1
分享到:
评论

相关推荐

    使用ROR编写ORACLE WEB应用

    在处理Oracle数据库查询时,ActiveRecord的查询接口使得SQL操作变得简单,如`.find`, `.where`, `.all`等。 部署是另一个关键环节。确保服务器环境也安装了必要的依赖,并正确配置了Oracle数据库连接。Rails应用...

    基于sqlite的ror例子

    @user = User.find_by(email: "john@example.com") ``` 更新用户信息: ```ruby @user.update(name: "Jane Doe") ``` 删除用户: ```ruby @user.destroy ``` 六、使用压缩包中的资源 "multiple_sqlite"这个...

    fusion图标使用手册.pdf

    如代码所示,我们先设定开始和结束日期,然后根据`params[:id]`获取员工ID,通过`Employee`模型的`find_with_timesheets_in_date_range`方法查询数据。 在`Employee`模型中,`find_with_timesheets_in_date_range`...

    享受将SQL注入RubyonRails应用程序的乐趣吧!___下载.zip

    1. **参数化查询**:ActiveRecord的查询接口使用方法链,如`find_by`,`where`等,它们会自动处理SQL注入。你应该避免使用字符串拼接构建查询。 2. **白名单参数**:对用户输入进行验证,只允许特定格式的数据。 3...

    Nocos启动异常.zip

    在IT行业中,尤其是在Java开发领域,我们经常遇到各种运行时异常。"Nacos启动异常"是一个常见的问题,这里出现的具体异常是`java.lang.UnsatisfiedLinkError`,它通常发生在试图加载本地(C/C++)库时找不到相应的库...

    template_for_shower:使用 Express 4 的 Node.js 应用程序,用于使用 Shower (https) 构建演示文稿

    template_for_shower 使用 Express 4 的 Node.js 应用程序,用于使用 Shower ( ) 构建演示文稿 在本地运行 确保您已安装 git clone ... cd template_for_shower npm install npm start 您的应用程序现在应该在上...

    ABB_PLC指令手册

    4. **循环右移指令(ROR)**:在指定范围内循环右移数值。 ### 数据选择与比较指令 1. **选择指令(SEL)**:根据条件选择两个数值中的一个。 2. **最大值指令(MAX)**:返回两个数值中的最大值。 3. **最小值...

    S7-200-PLC西门子指令中英文全称对照

    TBL_FIND(Table Find表格查找)**: 表查找。用于在指定的表格中查找特定的数据项。 #### 八、数据转换指令 **64. BCD_I(Binary Coded Decimal_I二进制编码的十进制)**: BCD码转整数。用于将BCD编码转换为整数格式...

    Q公共指令篇

    - **循环指令**:如ROL(循环左移)和ROR(循环右移)。 - **移位指令**:如SFTL(左移)和SFTR(右移)。 - **位处理指令**:如BIT_SET(置位)。 - **数据处理指令**:如ADD(加法)。 - **结构体创建指令**:如...

    Grails 技术精解与Web开发实践【源码+样章】----下载不扣分,回帖加1分,欢迎下载,童叟无欺

    20.1.5 改进的findBy 245 20.2 对插件系统的改进 245 20.3 数据绑定 245 20.4 在GSP中使用JSP的标签 246 20.5 加密配置文件中的数据库密码 246 20.6 本章小结 246 参考文献 247 索引 248 Grails技术精解与Web...

    Ruby on Rail 基础知识 一张纸

    - `find_all`: 查找所有符合条件的元素。 - `grep`: 匹配元素。 - `include?`: 包含元素。 - `max`: 获取最大值。 - `min`: 获取最小值。 - `reject`: 拒绝符合条件的元素。 - `sort`: 排序元素。 #### 三、...

    PowerPro_指令与功能块

    **PowerPro** 的外部指令主要由不同的库提供,这些指令能够实现更高级的功能,如字符串处理、位操作、版本信息查询等。 ##### 1. 字符串处理指令 - **结合字符串指令 (CONCAT)**:连接两个或多个字符串。 - **删除...

    西门子Splc指令系统推选PPT文档.ppt

    - **移位和循环移位指令**:如SHL(左移),SHR(右移),ROL(循环左移),ROR(循环右移)。 2. **置位域/复位域指令**: - S指令用于置位,将指定的地址范围设为1。 - R指令用于复位,将指定的地址范围设为0...

    Ruby+for+Rails

    例如,`Book.find_by(title: '某书名')`会查找标题为指定值的书籍。 9. ** erb模板** ERB(Embedded Ruby)是Rails视图中常用的模板引擎,可以插入Ruby代码到HTML中。例如,`&lt;%= @book.title %&gt;`会显示书籍的标题...

    Bochs - The cross platform IA-32 (x86) emulator

    [3078995] ROL/ROR/SHL/SHR modeling wrong when dest reg is 32 bit [2864794] BX_INSTR_OPCODE in "cpu_loop" causes crash in x86_64 host [2884071] [AIX host] prefetch: EIP [00010000] &gt; CS.limit [0000...

    The Art of Assembly Language Programming

    RCR 6.6.3.3 - ROL 6.6.3.4 - ROR 6.6.4 - The Bit Operations 6.6.4.1 - TEST 6.6.4.2 - The Bit Test Instructions: BT, BTS, BTR, and BTC 6.6.4.3 - Bit Scanning: BSF and BSR 6.6.5 - ...

Global site tag (gtag.js) - Google Analytics