Reputation: 7081
I have a Rails 3 blog, which has Article and Comment models in has_many
and belongs_to
associations. When an article comment is > 3, I should see it in my application.html.erb views so I can call it "most commented".
<div class="span2">
<%=image_tag ("Lekki_Gardens_new.gif") %>
<br><br>
<b><p>News update</p></b>
<% @articles.first(4).each do |article| %>
<%=image_tag article.avatar_url(:lilthumb).to_s, :class=>"img-polaroid" %><br>
<%= link_to article.name, article%><hr>
<% end %>
</div
Upvotes: 0
Views: 52
Reputation: 8034
You can use the :counter_cache
option in your article model and then use a scope to retrieve the most commented ones.
class Article < ActiveRecord::Base
has_many :comments, counter_cache: true
scope :most_commented, where('comments_count > 3')
end
And then in your template:
<% Article.most_commented.each do |article| %>
<% # anything you want %>
<% end %>
Upvotes: 3