This project is archived and is in readonly mode.

#4893 ✓invalid
Thiago

Rails 2 AR#first method

Reported by Thiago | June 17th, 2010 @ 09:25 PM

params params[:id] = 3
method Person.first(params[:id])

in Mysql, "where id='3'"
in sqlite3 "where id='1'"

sqlite3 doesn't consider the parameter

Comments and changes to this ticket

  • Jeff Dean

    Jeff Dean June 19th, 2010 @ 03:13 AM

    The first method ignores that id in all database drivers. first just adds a limit 1 to the query - so in your case, it looks like in your mysql database, the first Person record happened to have an id of 3. Here's what I get in both mysql and sqlite3:

    >> Post.create!
    => #<Post id: 1, title: nil, created_at: "2010-06-19 02:09:11", updated_at: "2010-06-19 02:09:11">
    >> Post.create!
    => #<Post id: 2, title: nil, created_at: "2010-06-19 02:09:14", updated_at: "2010-06-19 02:09:14">
    >> Post.create!
    => #<Post id: 3, title: nil, created_at: "2010-06-19 02:09:17", updated_at: "2010-06-19 02:09:17">
    >> Post.first
    => #<Post id: 1, title: nil, created_at: "2010-06-19 02:09:11", updated_at: "2010-06-19 02:09:11">
    >> Post.first 3 # => the "3" is totally ignored
    => #<Post id: 1, title: nil, created_at: "2010-06-19 02:09:11", updated_at: "2010-06-19 02:09:11">
    

    If you want to get the record by id, just use find:

    Person.find params[:id]
    

    If you want to find the first with some conditions, pass the conditions:

    Person.first :conditions => {:id => 3}
    
  • José Valim

    José Valim June 21st, 2010 @ 10:22 AM

    • State changed from “new” to “invalid”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages