yakryder / muscle-wizards

Contest prep app
0 stars 1 forks source link

A suggestions for user.preps #67

Closed dummied closed 8 years ago

dummied commented 8 years ago

https://github.com/SherSpock/muscle-wizards/blob/master/app/models/user.rb#L6

At the moment, it looks a user doesn't actually have many preps in the Rails sense (as in: based on user_id on a prep). And I don't see a belongs_to :user on Prep, but rather two relationships to users.

It might make sense to remove has_many :preps on User and go with something like this instead:

def athleted_preps
  Prep.where(athlete: self)
end

def coached_preps
  Prep.where(coach: self)
end

def preps
  athleted_preps.or(coached_preps)
end

This would then allow you to simplify in a bunch of other areas. prep_includes_user?(user) for instance.

  def prep_includes_user?(user)
    current_user.coached_preps.where(athlete: user).any?
  end

Bonus: this is now all done through SQL.

sbaughman commented 8 years ago

closed with commit on debug branch