For a project, who is an API, I want to be able to create "event" with a start, and a end date.
Sending datetime such as "Mon, 18 May 2015 12:56:10 UTC +00:00", isn't really easy, and it's a nightmare to parse in a front app.
What I want to achieve, is to allow my user to send a timestamp, and convert it to a datetime in DB.
However, has my fields are declared has Datetime
in DB, when I try to create a new row with a timestamp, it's not working and return nil for those fields.
Event.create(start_at: Time.now.to_i, end_at: Time.now.to_i)
#<Event:0x007ff02424c838
id: nil,
start_at: nil,
end_at: nil>
So far, I'd this :
before_validation :start_end_to_date
def start_end_to_date
self.start_at = Time.at(self.start_date).to_time
self.end_at = Time.at(self.end_date).to_time
end
In this case, my users send a start_date
params, and I convert it to a Datetime
.
Can you help me ?