Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails 2 AR#first method

#4893

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

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Dean
    Jeff Dean

    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}
    

    June 19th, 2010 @ 03:13 AM

  2. José Valim
    José Valim
    • State changed from new to invalid

    June 21st, 2010 @ 10:22 AM