Gibson
Gibson

Reputation: 2085

Chaining scope methods in rails

I have a problem when uploading new photos.

I have a scope named :most_tagged, which works apparently fine. Photos are shown by most_tagged DESC, but, when I upload a new photo that has 0 tags, it goes to the top(shows before the most tagged photo) instead than going to the bottom and show the last after the less tagged photo.

Should I chain another method to the most_tagged scope?

class Photo < ActiveRecord::Base
  has_many :tags , dependent: :destroy
  before_destroy { |record| record.tags.destroy_all if record.tags.any? }

  scope :most_tagged, -> { order('tags_count DESC') }
  scope :most_liked, -> { order('cached_votes_up DESC') }

Thanks

Upvotes: 2

Views: 169

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

You have to use the NULLS LAST option:

scope :most_tagged, -> { order('tags_count DESC NULLS LAST') }

Upvotes: 2

Related Questions