Where Is the Rails Method That Converts Data from 'Datetime_Select' into a Datetime Object

Where is the Rails method that converts data from `datetime_select` into a DateTime object?

The start of that code path, seems to be right about here:

https://github.com/rails/rails/blob/d90b4e2/activerecord/lib/active_record/base.rb#L1811

That was tricky to find! I hope this helps you find what you need

Rails - Comparing Datetime objects (date between two form dates)

You've used datetime_select and it doesn't pass everything in one params[:timeMin], you first have to convert it into DateTime object like this

timemin = DateTime.new(params["timeMin(1i)"].to_i,
params["timeMin(2i)"].to_i,
params["timeMin(3i)"].to_i,
params["timeMin(4i)"].to_i,
params["timeMin(5i)"].to_i)

In the same way you need to convert :timeMax into DateTime object. Then you can compare both.

DateTime object + hours|days|months from string method

You are trying to invoke the method '2.days' on self (the controller, I guess), but you need to call the 'days' method on the integer object returned by task_form.remind_value

Try this:

remind_time = task_form.start_at + task_form.remind_value.send(task_form.remind_space)

Rails Convert date_select to an int value

Heres how I solved it,

In my ActiveRecord model I added the fields

attr_accessible :my_int_date_time

columns_hash["my_int_date_time"] = ActiveRecord::ConnectionAdapters::Column.new("my_int_date_time", nil, "DateTime")

def my_int_date_time
return TimeHelper.datetime_from_integer(self.my_int_date)
end

def my_int_date_time= val
self.my_int_date = TimeHelper.datetime_to_integer(val)
end

In my form I then linked the datetime field to the field my_int_date_time rather than linking it to my_int_date

Getting range of dates from a date object in ruby

You are doing this:

p_end_date = params[:new_basecamp_project][:end_date]
p_end_date.to_s

This does not convert p_end_date to a string object. If you reassigned it, it would.

p_end_date = p_end_date.to_s

Also p_end_date = params[:new_basecamp_project][:end_date] does not create a DateTime object, when you do a save to the database it will create those DateTime object on save. You will have to do that manually if you want to manipulate them in this way before the save. Depending on how you are getting the Date in your form (date picker, date hash, etc) you will need to create the DateTime object. You can see some examples here: Where is the Rails method that converts data from `datetime_select` into a DateTime object?

So the easiest thing is to save the record in the DB, THEN you can select the record from the DB like:

record = SomeModel.last

and then use the two methods to create the range:

(record.project_start_date..project_end_date).each do |date|

How to handle date/times in POST parameters?

In the database, all dates are always saved as UTC. The config.time_zone parameter is setting the default value for Time.zone, just for display. So you don't have to do anything to get the behavior you desire, just keep Time.zone set correctly for the user, and rails takes care of the rest.

How to create a new DateTime object in a specific time zone (preferably the default time zone of my app, not UTC)?

You can use ActiveSupport's TimeWithZone (Time.zone) object to create and parse dates in the time zone of your application:

1.9.3p0 :001 > Time.zone.now
=> Wed, 11 Jul 2012 19:47:03 PDT -07:00
1.9.3p0 :002 > Time.zone.parse('2012-07-11 21:00')
=> Wed, 11 Jul 2012 21:00:00 PDT -07:00

Transform DateTime into simple Date in Ruby on Rails

DateTime#to_date does exist with ActiveSupport:

$ irb
>> DateTime.new.to_date
NoMethodError: undefined method 'to_date' for #<DateTime: -1/2,0,2299161>
from (irb):1

>> require 'active_support/core_ext'
=> true

>> DateTime.new.to_date
=> Mon, 01 Jan -4712


Related Topics



Leave a reply



Submit