Topic: Mixing Logic in Models
I have this query in my view page that finds all the users for the site, orders them by last name and then checks to see if that user has "0" projects associated with them. If this is true then we spit out the names of those people.
<h1>Users who don't have projects assigned to them</h1>
<% for user in User.find(:all, :order=>'last_name') %>
<% if user.projects.count == 0 %>
<%= user.first_name %><br>
<% end %>
<% end %>
I look at this code and I think this type of query is not DRY and should be in a model and have this type of method.
class User < ActiveRecord::Base
# My attempt would be to put the code within this model file
# I just can't figure out how could I get this "user.projects.count == 0" inside this method/model?
I just can't figure out how could I get this "user.projects.count == 0" inside this method/model? Can that logic be put into this model/method? OR Is it best practice to put this type of logic inside of a controller or view?
Looking to see how someone else might approach this?