Last record

hi @all

Can anyone help me? I would like to read the last two records from a
database. In the database I have a table with id, name and sex.

How can I read the last two records from the entered names, who are
female? There are two conditions I need:

last entries

User.find(:all, :condition…) ??

Well… you could add at created_at field to the table, then order by
and retrieve the last two rows.

User.find(:all, :order => ‘created_at desc’, :limit => 2)


@last_two_users = User.find(:all, :conditions => “sex-condition =
‘female’”, :order => “created_at DESC”)

assuming you are storing created_at time, right? If not, you could
order by “id DESC”.

That should do it.

M. R. wrote:

User.find(:all, :condition…) ??
How about this (not tested):

count = User.count(:conditions => “…”)
User.find(:all, :conditions => “…”, :limit => 2, :offset => count-2)

Istvan Hoka

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs