Find :last

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

There is no Model.find(:first).

You need to do Model.find(:first, :order => '?? DESC')

Pål Bergström wrote:

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

Do you have a column "id" or something to index with?

Maybe, try find_by_id or find_by_column-name(:last)

Justin To wrote:

Pål Bergström wrote:

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

Do you have a column "id" or something to index with?

Maybe, try find_by_id or find_by_column-name(:last)

Not sure what you mean. I have and id, yes. I'm just trying to find that
last record with :last, the opposite to :first.

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

Pål Bergström wrote:

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

There's no option :last. Instead, find(:first) but specify an order to
reverse them. For example:

Model.find(:first, :order => 'created_at desc')

This will fetch the most recently created item.

Jeremy Weiskotten wrote:

Pål Bergström wrote:

When I use Model.find(:last) I get an error saying that it can't find an
ID=last. How come?

There's no option :last. Instead, find(:first) but specify an order to
reverse them. For example:

Model.find(:first, :order => 'created_at desc')

This will fetch the most recently created item.

Hmm. From the api:

# find last
  Person.find(:last) # returns the last object fetched by SELECT * FROM
people
  Person.find(:last, :conditions => [ "user_name = ?", user_name])
  Person.find(:last, :order => "created_on DESC", :offset => 5)

Craig White wrote:

Interesting, that was added in March. What version of Rails are you
using? I'm guessing you need 2.1.

Craig White wrote:
>> When I use Model.find(:last) I get an error saying that it can't find an
>> ID=last. How come?
> ----
> not an implemented option
>
> http://rails.rubyonrails.org/classes/ActiveResource/Base.html#M000933
>
> Craig

I understand. Why is it in there?

Jeremy Weiskotten wrote:

Interesting, that was added in March. What version of Rails are you
using? I'm guessing you need 2.1.

I see. I run 2.0.2. That explains it.

at least it fails with postgres...perhaps some of the SQL adaptors can
make sense of the query

Craig

Working for me with postgres 8.3.1 with or without options.

Even if you're using a version of Rails that doesn't have the
find(:last) option, you can essentially pull of the same thing with:

find(:first, :order => 'id desc')

I think that may even be what the Rails method does.

-- Josh

Jeremy Weiskotten wrote:

Joshua Abbott wrote:

Even if you're using a version of Rails that doesn't have the
find(:last) option, you can essentially pull of the same thing with:

find(:first, :order => 'id desc')

I think that may even be what the Rails method does.

-- Josh

Yes, I know. I normally use that. Then I saw :last in the api.

With me Model.first works... Model.last works as well... no need for
Model.find...