Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: expected numeric

I have one model with the name job.

I want to calculate the number of days since the job has been created.

I think we have to calculate the difference between Date.today and jobs created_at

my method is:

def self.open_jobs(date)
    Date.today - where("status='open' AND date(created_at) = ?",date).round
end

I am getting some error please let me know how to calculate the days the job has been created.

I have to calculate number of days. for example I have created a job on 20th sep 2012. it should be Date.today - 20/09/2013 answer should be 5 days and one more thing i need to check one more column status ="open" and display only those jobs.

if anybody knows how to calculate please post here.

like image 339
Sowmya M R Avatar asked Sep 23 '13 07:09

Sowmya M R


3 Answers

job.created_at is a Time, Date.today is a Date

You can't subtract Time and Date. See that Date.tomorrow - Time.now gives you that error.

To get the difference in seconds:

Time.now - job.created_at

You can convert this to days (float or integer) as follows:

(Time.now - job.created_at) / 24.hours
((Time.now - job.created_at) / 24.hours).to_i

Alternately there is (MyGod's answer):

Date.today.mjd - job.created_at.to_date.mjd
like image 74
MaximusDominus Avatar answered Nov 19 '22 17:11

MaximusDominus


You're right that you should use Date.today and job.created_at

If you want to calculate the number of days since the job has been created use this:

def job_for_days
  (Date.today - self.created_at).round(1.day)
end
like image 39
dax Avatar answered Nov 19 '22 19:11

dax


Given two dates-

date1= Date.parse("10/10/2012")

date2= Date.parse("10/21/2012")

The difference in days:

date2.mjd - date1.mjd

where,

mjd = Modified Julian Day Number

like image 1
My God Avatar answered Nov 19 '22 18:11

My God