0

I have my scope:

scope :latest_photos, -> {order(:created_at).reverse_order.limit(10)}

It's supposed to put the latest photos in first place right? Well photos get put on the last place instead.

I've also tried:

scope :latest_photos, -> {order('created_at DESC').limit(10)} 

but nothing. Any ideas? Thanks!

EDIT

Something is not working here:

routes.rb

get 'spots/ultimos' => 'photos#latest

photos controller

def latest
        @categories = Category.all
        @zones = Zone.all
        @photos = Photo.latest_photos
        @action = 'general'
        @photos = Photo.paginate(:page => params[:page])
        render :index
    end

model

  scope :latest_photos, -> {order(created_at: :desc).limit(10)}
Community
  • 1
  • 1
Gibson
  • 2,055
  • 2
  • 23
  • 48

1 Answers1

3
def latest
    @categories = Category.all
    @zones = Zone.all
    @photos = Photo.latest_photos
    @action = 'general'
    @photos = Photo.paginate(:page => params[:page])
    render :index
end

You have assigned @photos variable twice, second assignment overrides the previous one. Instead do:

def latest
    @categories = Category.all
    @zones = Zone.all
    @photos = Photo.latest_photos
    @action = 'general'
    render :index
end

The actual value to be assigned depends on what you want to achieve here. Since action is called latest and you have limit in your scope, I have assumed you don't need pagination here.

BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • But then, how can I paginate photos? I need to. Like this? @photos = Photo.latest_photos.paginate(:page => params [:page]) ? – Gibson Jun 09 '14 at 13:28
  • Yep, that worked, I was overriding photos , what I did in order to preserve pagination is: @photos = Photo.latest_photos.paginate(:page => params [:page]) – Gibson Jun 09 '14 at 13:31
  • @Gibson - If you want pagination, than this is correct. – BroiSatse Jun 09 '14 at 13:31