steveyang
steveyang

Reputation: 9298

How to check whether a model has a scope defined?

class Idea < ActiveRecord::Base
  attr_accessible :archived, :checked, :content, :note, :stared

  scope :stared, -> { where(stared: true) 
end

With this code, how could I test whether a scope (such as stared) is defined on Idea. I want to have this effects

Idea.has_scope?(:stared) 
=> true
Idea.has_scope?(:unknown)
=> false

Upvotes: 2

Views: 145

Answers (2)

Nishant
Nishant

Reputation: 3005

There is a method :valid_scope_name which internally also uses respond_to? + is also a protected method + gives a terrible log message. It can be invoked as

Idea.send(:valid_scope_name?,:stared)
=> true

But this would also mostly, be removed from the edgerails - git commit

Upvotes: 0

Chris Barretto
Chris Barretto

Reputation: 9529

You could just use respond_to?

Idea.respond_to?(:stared)

Will yield true/false

Upvotes: 1

Related Questions