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 > sex-condition User.find(:all, :condition.....) ?? -- Posted via http://www.ruby-forum.com/.
Well.... you could add at created_at field to the table, then order by that and retrieve the last two rows. User.find(:all, :order => ''created_at desc'', :limit => 2) -Jonathan. On 8/14/06, M. R. <ribi@schwingerverband.ch> wrote:> > 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 > > sex-condition > > User.find(:all, :condition.....) ?? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060814/4d9400c5/attachment.html
M. R. wrote:> 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 > > sex-condition > > User.find(:all, :condition.....) ??How about this (not tested): count = User.count(:conditions => "...") User.find(:all, :conditions => "...", :limit => 2, :offset => count-2) -- Istvan Hoka
@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. On 14/08/06, M. R. <ribi@schwingerverband.ch> wrote:> 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 > > sex-condition > > User.find(:all, :condition.....) ?? > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >