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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -