Sql 在使用范围时,是否有任何方法可以从特定记录中偏移

Sql 在使用范围时,是否有任何方法可以从特定记录中偏移,sql,ruby-on-rails,activerecord,offset,Sql,Ruby On Rails,Activerecord,Offset,我有很多方法在特定的活动记录之后查找记录。例如: def photo_recent offsetphoto = Photo.find(params[:id]) @photos = Photo.recent.where('created_at> ?', offsetphoto.id).limit(10)#recent is a scope using created_at end def photo_recent offsetphoto = Photo.find(p

我有很多方法在特定的活动记录之后查找记录。例如:

def photo_recent
    offsetphoto = Photo.find(params[:id])
    @photos = Photo.recent.where('created_at> ?', offsetphoto.id).limit(10)#recent is a scope using created_at
end

def photo_recent
    offsetphoto = Photo.find(params[:id])
    @photos = Photo.popular.where('like_count > ?', offsetphoto.like_count).limit(10)#popular is a scope using like_count
end
我想知道是否有任何方法可以将其模块化,例如:

@photos = Photo.recent.offset(Photo.find(params[:id])).limit(10)

您可以编写一个范围,它只获取当前照片中的所有内容

scope :offset_from, -> (photo) { where('id >= ?', photo.id) }

Photo.offset_from(Photo.find(params[:id]))......

您可以编写一个范围,它只获取当前照片中的所有内容

scope :offset_from, -> (photo) { where('id >= ?', photo.id) }

Photo.offset_from(Photo.find(params[:id]))......
示例用法 示例用法
photo = Photo.find(123)
ten_next_created_photos_after_photo = Photo.most_recent_starting_from(photo).limit(10)