I have a rails 4 form in which a user inputs a string that represents a datetime:
<%= f.text_field :time_frame, class: "calender_pop" %><br />
No where in my code am I converting the string into a datetime. I have tried entering non-datetime input into the field (i.e. integers, strings, etc...) but datetime won't accept them. If i try to enter a non-datetime value, datetime becomes nil. This is good, but I am confused because I never wrote any such validation. I have used postgres before and have been able to put integers into string columns, so I am not aware of any automatic validations on the part of postgres or rails.
- Does postgres or rails automatically validate datetime?
- Is the time_frame column being filled with a string or a datetime?
I am also using the query datetimepicker plugin, but i don't think that should have anything to do with datatypes.
UPDATE:
I ran:
Task.last.time_frame.is_a? Time
and it returned true.
So question 3. What is happening? How and when is it getting converted into a Time?
Update:
I just read that postgres actually does not have a datetime type. Could it have something to do with the orm changing the datatype?
1.- I wouldn't call that a validation. It's being parsed as a Time, as you've already been able to see. As long as it's parseable as a Time (I'm guessing it uses Time.parse) it will give Time back. Validations are rules that constraint the subset of valid Times. A validation would be, for instance, to check that only past times are accepted.
2.- If the column is defined as a datetime in postgres, it can't be filled with anything else than a datetime no matter what Rails does.
3.- I'm going to guess that you're using form_for here. Since you're passing an instance of an object here, the rails form helper knows the type of each attribute. So, when you ask it to give you a text field for that attribute, which is actually a datetime (rails parses this information from the db), he knows it has to be converted. Now, when exactly does this happens, I don't know. But I'm going to guess the helper is smart enough to call time_field instead of text_field:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-time_field
You can check this by looking at the generated HTML.