在Rails中暫時取消default_scope來搜尋資料
2012-03-15 22:17:42 +0800 由 hechian
在 Ruby on Rails 節點 中發起
最後由 ricky 於2012-03-27 15:19:01 +0800回應
, 303次閱讀
# category.rb
has_many :articles
# article.rb
belongs_to :category
default_scope where(deleted: false).order("id DESC")
def most_popular_articles
articles.unscoped.where(deleted: false).order("visitor_counter DESC")
end
unscoped
不只可以直接用在Article上,也可以搭配關連使用;加上unscoped
可以在該次查詢中取消defaut_scope
的設定