Ruby on rails - Showing featured posts? -
i creating blog application ruby on rails , show 30 'featured' articles on home page.
i have considered using boolean attribute 'featured' on articles table, have issues this;
- if have 4000 articles, have search through of them find 30 featured.
- 3970 articles have empty column.
i'm wondering if there better way - such creating db table stores id's of featured articles?
thanks in advance.
i think in case best decision use model scope featured articles.
class article < applicationrecord scope :featured, -> { where(featured: true) } end
and use scope rails view
<% @articles.featured.each |article| %> <p><%= link_to article.subject, article_path(article) %></p> <% end>
link scopes ruby on rails guide
Comments
Post a Comment