Reputation: 1287
I have a scope which uses a where clause to narrow the results down to records where a specific parameter is defined.
In Post.rb
scope :nav, where( "priority IS NOT NULL" )
Im using this scope to populate the site navigation with specific Posts. However, the index controller action needs to omit these records. Is there a way I can use the same named scope to omit these records from my index action? Or do I need to write a second scope which is the inverse of the first?
def index
@posts = Post.order('created_at DESC')
end
Upvotes: 0
Views: 67
Reputation: 790
In Post.rb
scope :nav, ->(not_null = true) { not_null ? where.not(priority: nil) : where(priority: nil) }
In controller:
def index
@posts = Post.order(created: :desc).nav(false)
end
Work in Rails 4.
Upvotes: 1