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;

  1. if have 4000 articles, have search through of them find 30 featured.
  2. 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

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -