Find_all / find_first deprecated : a question

was reading
http://glu.ttono.us/articles/2006/08/30/guide-things-you-shouldnt-be-doing-in-rails
getting this rails app ready for 2.0 …

anyways, here’s a small snippet that i’m wondering about:


Using Deprecated Finders

find_all and find_first have been deprecated over a year now. Stop
using them. While you’re at it, stop using render_partial. Their
better educated cousin find can handle all their needs. Use
find(:first), find(:all) and render :partial

i can understand using find(:all) instead of find_all … but what about
find_all_by_field_name … i love that little nugget. it couldn’t
possibly be deprecated could it?

thanks

No, I believe those are here to stay.

~Jamie

Hi, I would recommend referencing the API docs for Rails at the
following:

http://api.rubyonrails.org/

Good luck,

-Conrad

Hi, in regards to API documents, you should be using the following
instead
of find_all and find_first:

find(*args)

Find operates with three different retrieval approaches:

  • Find by id: This can either be a specific id (1), a list of ids (1,
    5, 6), or an array 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
    no record can matched, nil is returned.
  • Find all: This will return all the records matched by the options
    used. If no records are found, an empty array is returned.

All approaches accept an option hash as their last parameter. The
options
are:

  • :conditions: An SQL fragment like “administrator = 1” or [
    “user_name = ?”, username ]. See conditions in the intro.
  • :order: An SQL fragment like “created_at DESC, name”.
  • :group: An attribute name by which the result should be grouped.
    Uses the GROUP BY SQL-clause.
  • :limit: An integer determining the limit on the number of rows that
    should be returned.
  • :offset: An integer determining the offset from where the rows
    should be fetched. So at 5, it would skip the first 4 rows.
  • :joins: An SQL fragment for additional joins like “LEFT JOIN
    comments ON comments.post_id = id”. (Rarely needed). The records will
    be returned read-only since they will have attributes that do not
    correspond
    to the table’s columns. Pass :readonly => false to override.
  • :include: Names associations that should be loaded alongside using
    LEFT OUTER JOINs. The symbols named refer to already defined
    associations.
    See eager loading under Associations.
  • :select: By default, this is * as in SELECT * FROM, but can be
    changed if you for example want to do a join, but not include the
    joined
    columns.
  • :readonly: Mark the returned records read-only so they cannot be
    saved or updated.

Examples for 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, :order => “created_on DESC”, :offset => 5)

Examples for 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, :offset => 10, :limit => 10)
Person.find(:all, :include => [ :account, :friends ])
Person.find(:all, :group => “category”)

Good luck,

-Conrad