This project is archived and is in readonly mode.
Rails 2 AR#first method
-
Jeff Dean
The
firstmethod ignores that id in all database drivers.firstjust adds alimit 1to 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
- State changed from new to invalid