find the last two records with a certain field value

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

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)