I have a situation where a code can be toggled on/off so a record is sent to database with a value of 1 or 2 for code. So lets say it was toggled 4 times: id, code, time 1, 1, 2012-05-31 22:05:24 2, 2, 2012-05-31 22:05:25 3, 2, 2012-05-31 22:05:26 4, 2, 2012-05-31 22:05:27 So it was toggled on once and toggled off 3 times. Now I would like to use rails 3 in order to find the last record toggled to off and the last record toggled to on. Something like this below, but this would pull the last two records that meet either criteria, which is not what I want: where{code.in([1, 2])}.order(''time desc'').limit(2) thanks for response -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
John Merlino
2012-Jun-01 19:57 UTC
Re: find the last two records with a certain field value
I''m basically looking for something more elegant than this: item = [] item << last_on = where{:code => 8}.order(''time desc'').limit(1) item << last_off = where{:code => 9}.order(''time desc'').limit(1) On Jun 1, 3:23 pm, John Merlino <stoici...-YDxpq3io04c@public.gmane.org> wrote:> I have a situation where a code can be toggled on/off so a record is > sent to database with a value of 1 or 2 for code. So lets say it was > toggled 4 times: > > id, code, time > 1, 1, 2012-05-31 22:05:24 > 2, 2, 2012-05-31 22:05:25 > 3, 2, 2012-05-31 22:05:26 > 4, 2, 2012-05-31 22:05:27 > > So it was toggled on once and toggled off 3 times. > > Now I would like to use rails 3 in order to find the last record > toggled to off and the last record toggled to on. > > Something like this below, but this would pull the last two records > that meet either criteria, which is not what I want: > > where{code.in([1, 2])}.order(''time desc'').limit(2) > > thanks for response-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.