Refactor sorting of images

Hi guys,

I am a bit stuck here, I know the following is damn wrong, but I
couldn't figure out
a cleaner solution yet.

I have a bunch of images, each one has a published date and a 'liked'
A user can click like from an image once.
The index of the images can be sorted after 'liked', and 'published'.

The show from an image has a next and prev button, so that you can
browse through the all images.
A picture has a next_picture and prev_picture method, so that it can
fetch it's successor and predecessor.

## pictures_controller
def index
@pictures = Picture.where(:state => 'published').paginate :page =>
params[:page], :per_page => per_page, :order => "#{sort_order.to_s}

## pictures model
  def prev_picture
    prev_picture = self.class.where(sort_order.gte =>
send(sort_order), => uid, :state =>
    return nil if prev_picture.blank?
    if send(sort_order).eql?(prev_picture.send(sort_order))
      pictures = self.class.where(:state =>
'published').order("#{sort_order.to_s} DESC").all
      (pictures.index(self) == 0) ? nil :

Ok, the next_piture method looks very similar. Basically this will
fetch the previous picture from the database. If the sort_order
attribute from both are the same,
it will fetch all pictures and grabs the one in the array before the
current one.
I tried the whole thing with a second order parameter, but it really
get's complicated, as soon as I have to fetch the previous or next