`
s00n
  • 浏览: 20324 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

Rails经常犯的错误一 - find(:first)

 
阅读更多

欢迎大家来这里坐坐,   RubyOnRails话吧

( http://www.3user.com/group/index/12015 )


做个测试,建个表
CREATE TABLE user(
  id INT NOT NULL AUTO_INCREMENT,
  user_name varchar NOT NULL

)

test的fixtures

这么写

one:
  id: 1
  user_name: a
two:
  id: 2
  user_name: b

 

然后在user_test.rb中写一个测试方法如下:

  def test_find
   p User.find(:first,:conditions =>["id=?", 2]).id                   #正确的写法
   p User.find(:first,["id=?", 2]).id                                          #错误的写法
  end

打印出来的结果

2             #正确的结果

1             #与预期不一样的结果

查看日志如下:

Nov 04 20:03:58 13cc97e4810e43f rails[3076]: User Load (0.000000)   SELECT * FROM user WHERE (id=2) LIMIT 1
Nov 04 20:03:58 13cc97e4810e43f rails[3076]: User Load (0.000000)   SELECT * FROM user LIMIT 1

错误已经很明显了,如果不写":conditions:=>",前面查询有:first,则会忽略后面写的条件.

我犯过几次这样的错误了,怎么看都查不出问题,程序也不报错。

特写此文,纪念一下曾经让我郁闷的code.

 

欢迎大家来这里坐坐,   RubyOnRails话吧

( http://www.3user.com/topic/show/12462 )


分享到:
评论

相关推荐

    Ruby on Rail 基础知识 一张纸

    - `first`: 获取第一个元素。 - `flatten!`: 展平多维数组。 - `include?`: 判断数组是否包含指定元素。 - `index`: 查找指定元素的位置。 - `indexes`: 查找多个元素的位置。 - `join`: 使用分隔符连接数组...

    golang-gorm:golang与gorm练习

    GORM是Golang的一个ORM库,它的目标是提供简单的API用于数据库操作,类似于Ruby on Rails的ActiveRecord。GORM支持多种数据库,如MySQL、PostgreSQL、SQLite和SQL Server,允许开发者通过面向对象的方式处理数据库...

    Rails3的ActiveRecord 查询API.doc

    2. **`find(:first)` 和 `find(:all)`**:这两个方法已不再推荐,取而代之的是直接使用 `first` 和 `all`。值得注意的是,`count` 方法仍然接受 `:distinct` 参数。 3. **`named_scope`**:这个在 Rails 2.x 中广泛...

    ruby on rails 3.1.0数据库查询方法汇总

    ### Ruby on Rails 3.1.0 数据库查询方法汇总 #### 一、基础操作 在 Ruby on Rails 3.1.0 中,对于数据库的操作非常便捷,主要包括数据的保存、创建、查找等功能。 1. **保存数据**: - `a = Category.new(:name...

    lazy_find:简化了ActiveRecord中的first,last,take方法

    懒人查找 描述 Simplified the first,last,take methods in ... Find the first record (or first N records if a parameter is supplied). 旧语法: Person.where(:email => "jenorish@gmail").first 新语法:

    Rails 3中的Active Record的查询变化.doc

    对于查询第一条或最后一条记录,旧的`find(:first, :order => "published_at desc")`在Rails 3中可以写成`Article.order("published_at desc").first()`,或者考虑到效率和简洁性,可以使用`Article.order(...

    W5D5:W5D5 Classwork-Rails ActiveRecord查询

    在Ruby on Rails框架中,ActiveRecord是核心组件之一,它负责数据库操作并与模型(Model)进行交互。在“W5D5: W5D5 Classwork - Rails ActiveRecord查询”这个主题中,我们将深入探讨如何使用ActiveRecord进行有效...

    rails常用数据库查询操作、方法浅析

    Model.find(:first, options) Model.last Model.last(options) Model.find(:last, options) 通过id获取记录 代码如下: Model.find(1, 10, options) Model.find([1, 10], options) .find all 代码如下: Model.all...

    Ruby的25个编程细节(技巧、实用代码段)

    在Rails中,`find` 方法有许多变体,其中之一就是 `find(:first)`,用于查询符合条件的第一条记录。结合 `:conditions` 参数,我们可以指定查询条件。 **示例代码:** ```ruby mobile_info = MobileInfo.find(:...

    Ruby Performance Optimization, Why Ruby is Slow, and How to Fix It

    You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code–but that’s just the beginning. See exactly ...

    hello-sparkjava:使用mongodb的Spark Java

    Document doc = collection.find(Filters.eq("_id", id)).first(); if (doc != null) { return doc.toJson(); } else { return "No data found with given ID."; } }); ``` 为了确保应用能够正常运行,我们...

    Laravel开发-lara-crud

    Laravel由Taylor Otwell开发,它的设计灵感来源于Ruby on Rails。Laravel的特点包括但不限于:MVC(模型-视图-控制器)架构模式、强大的Eloquent ORM(对象关系映射)、Artisan命令行工具、单元测试支持、路由系统、...

    Castle ActiveRecord快速入门指南、ActiveRecord帮助文档

    - `First()`/`FirstOrDefault()`:获取第一条满足条件的记录。 - **查询语言**:支持 LINQ to NHibernate 和 HQL(Hibernate Query Language),用于编写复杂的查询语句。 5. **事务处理** - ActiveRecord 提供...

    Ruby-GuacamoleArangoDB的ODM框架

    Ruby-Guacamole是针对ArangoDB数据库的一个Object-Document Mapping (ODM)框架,它为开发者提供了一种更方便的方式来操作ArangoDB中的数据,类似于Ruby on Rails中的ActiveRecord。ODM允许开发者以对象的形式操作...

    active_record_patch_first_or_create

    AtomicFirstOrCreate ActiveRecord first_or_create替代方案,它在RecordNotUnique异常上重试。 first_or_create本身不能保证唯一性...正如Rails团队所记录的那样,诸如first_or_create或find_or_create方法不是原子

    jQAPI - Alternative jQuery Documentation - For Version 1.7

    Why another jQuery Documentation?Solving your own problems is so much fun. There are numerous alternative jQuery documentations ...*) Edit: it is the first search result when you enter it with brackets.

    LeanCloudSDK封装LeanMotion.zip

    LeanCloud ,一个为移动应用提供数据统计、数据存储、消息推送、发送短信、发送邮件等云服务商。 LeanMotion 是一个RubyMotion的Gem,可以更加方便地使用LeanCloud SDK。用更Ruby的写法来操作LeanCloud的数据,类...

Global site tag (gtag.js) - Google Analytics