sumeetjain / outcomes-tracker

0 stars 0 forks source link

Move .retrieve_all_for_user methods in Offer and Job Application models to the User model #30

Closed halfghaninne closed 8 years ago

halfghaninne commented 8 years ago

(Because, duh.)

Currently, both JobApplication and Offer models have a class method .retrieve_all_for_user(user). This should be instead moved into the User model.

Either with something simple on the class like: has_many :job_applications, through: entries (not sure if that association works that a User has many Entries, but an Entry only has one Job App)

Or writing a custom method like:

def job_applications
  entries = Entry.where(user_id: self.id)
  collection = []
  entries.each do |entry|
    if entry.job_application
      collection << entry.job_application
    end
    return collection
  end
end

The above would be repeated for offers (and later for positions, when they exist).

sumeetjain commented 8 years ago

Using either a Rails association (e.g. has_many) or a joins (or include) of some kind would be ideal here. The custom method you cited would be a very expensive one, as each entry would have to run entry.job_application which pings the database.