2

コンテンツの多いアプリがあります。現在、 というブール値がありfeaturedます。がマークされている場合feature、このコンテンツをページの上部に表示したいと考えています。現在、コンテンツのスコープ オーダーを設定していますが、注目のアイテムを最初に表示する方法がわかりません。何かご意見は?以下は、コンテンツ用に私が持っている小さなスコープです。

  scope :published, where(:published => true )
  scope :unpublished, where(:published => false )
  scope :ordering, order("published_date desc")
  scope :feature, where(feature: true )

コンテンツを表示するホームコントローラーは次のようになります。

  def index
    @contents = Content.includes(:author).published.ordering
    @collections = @user.try(:collections)
    @categories = Category.includes(:subcategories).all
  end

if else ステートメントよりもこれを行うためのより良い方法があるようです。提案?注文ドキュメントを調べましたが、このケースに固有のものは見つかりませんでした。

使用:
Rails 3.2.14
Ruby 1.9.3

4

2 に答える 2

1
scope :order_by_feature_then_published, order("contents.feature DESC").order("published_date DESC")


  def index
    @contents = Content.includes(:author).published.order_by_feature_then_published
    @collections = @user.try(:collections)
    @categories = Category.includes(:subcategories).all
  end

これは機能します。

于 2013-09-20T17:44:54.600 に答える