I have a model called Timesheet which has many TimesheetRows. TimesheetRow belongs to a job.
class Timesheet < ActiveRecord::Base
has_many :timesheet_rows, inverse_of: timesheet
end
class TimesheetRow < ActiveRecord::Base
belongs_to :timesheet, inverse_of: timesheet_rows
belongs_to :job
end
While building a timesheet object from logs, I am trying to check if timesheet_row corresponding to a job has been already built or not, as follows.
timesheet = Timesheet.new()
if timesheet.timesheet_rows.exists?(job_id:n)
#Do something
else
timesheet.timesheet_rows.build(job_id:n)
end
I have tried .exists?(condition) , .find(:all, condition) , .find_by_job_id(n) , .where(condition) etc. All query from database, and thus won't be useful here.
I have browsed for hours now, looking for some magic method but couldn't find any. Really, will I just have to loop through all the associations?
A similar question.
Thanks