2013-01-23 83 views
2
class Idea < ActiveRecord::Base 
    attr_accessible :archived, :checked, :content, :note, :stared 

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

使用此代碼,我如何測試範圍(如stared)是否在Idea上定義。我想要這個效果如何檢查模型是否定義了範圍?

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

回答

1

你可以使用respond_to?

Idea.respond_to?(:stared) 

將產生真/假

+0

它將測試比對其他領域的所有實例方法。 – steveyang

0

有一種方法:valid_scope_name其內部還採用了respond_to? +也是一種保護方法+提供了一個可怕的日誌信息。它可被調用

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

但這樣做也大多從edgerails被刪除 - git commit

相關問題