Answer the question
In order to leave comments, you need to log in
How to write the date to the database?
rails 4, sqlite
A field in a DATE type database. Gets from the database in the format yyyy-mm-dd. When rendering, I convert to dd.mm.yyyy. I edit in the browser in the same format. I send everything to the server in the same format dd.mm.yyyy. Attention to the question: How to convert to yyyy-mm-dd just before writing to the database, because rails wants to get the date in this format? If the question is clear, then you can not read further, otherwise you can get confused). In controller:
def update
@model.update_attributes(x)
end
def warranty
'08.03.2016'
end
Answer the question
In order to leave comments, you need to log in
In total, in the model code, you need to declare a getter and a setter:
attr_accessor :warranty
def warranty=(value)
write_attribute :warranty, value.to_date
rescue
#если строка, пришедшая в параметрах не валидна, в базу пойдет null
end
def warranty
self[:warranty].strftime '%d.%m.%Y' if self[:warranty]
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question