In Oracle, I would write the following:
YourModel.find(:conditions => 'MOD(ROWNUM,3) = 0')
this has the advantage that the filter occurs in the database, so not everything is retrieved.
In PostgreSQL, this is called ROW_NUMBER (which is actually an SQL standard). In MySQL, this is not supported.
In mysql, you can simulate rownum with SELECT @rownum: =@rownum +1 rownum, t.* FROM (SELECT @rownum:=0) r, mytable t; . So I think something like
Bar.find_by_sql("select * from (SELECT @rownum: =@rownum +1 rownum, t.* FROM (SELECT @rownum:=0) r, mytable t) where mod(rownum,3) = 0")
must work.
source share