Displaying model statistics (as a sentence) in a view A Rails 4.2 app has three models:


User
Author
Quote


... and the following associations:


users HABTM authors
author has_many quotes


If the user is associated with any authors, his "profile" view should display statistics about the number of quotes of each author.

Example:


  Your author(s) Mark Twain has 73 quotes, Charles Dickens has 92 quotes, and Oscar Wilde has 53 quotes.


If the user is not associated with any authors, that same view should display a different message.

Within the user model (app/models/user.rb):

def authors_statistics
  statistics = Array.new
  self.authors.each do |author|
    statistics &lt;&lt; "#{author.name} has #{author.quotes.count} quotes"
  end
  statistics.to_sentence
end


Within the user profile view (ERB):

&lt;% if current_user.authors_statistics.empty? %&gt;
    You have not yet been given access to edit any authors&amp;#39; quotes. Please
    &lt;%= link_to 'send us a message', new_contact_form_path %&gt; specifying to
    whose authors quotes you would like to contribute.
&lt;% else %&gt;
    Your author(s) &lt;%= current_user.authors_statistics %&gt;
&lt;% end.to_sentence %&gt;.


It seems to me that there is excessive coupling between the model and the view. Can you think of a better way to do it?
I'd add a counter cache to Author, i.e. quote_count just to avoid doing the extra query to fetch the number of quotes.

Don't put view concerns (the sentence, in this case) in a model; use I18n/localization instead, especially since you're dealing with pluralization (and right now you're taking the lazy route of saying author(s) and assuming quotes, plural). 

Even if there's only English, the I18n stuff can do some tricks, for instance (in config/locales/en.yml or similar):

en:
  users:
    show:
      your_authors:
        one: "Your author"
        other: "Your authors"
      author_quotes:
        one: "%{name} has %{count} quote"
        other: "%{name} has %{count} quotes"


With that your view can be something like:

&lt;%= t(".your_authors", count: @authors.count) %&gt;
&lt;%= @authors.map { |author| t(".author_quotes", name: author.name, count: author.quotes.size }.to_sentence %&gt;


And #author_statistics can be removed entirely.