0

I have a virtual attribute start_date and a database column start (datetime).

How can i override the date (but not the time) of start with the date from start_date?

I tried this in the setter method and the logger says that self.start/start is set, but in the database and also the update log start has still the old date.

# model: db table with columns: id(int), start(datetime)
class DateTimeTest < ActiveRecord::Base

  attr_accessible :start_date, :start

  def start_date=(value)
    logger.debug '>>>>' + start.inspect
    logger.debug '>>>>' + self.start.inspect
    start = DateTime.new(value.to_date.year, value.to_date.month, value.to_date.mday, 15,15,15)
    self.start = DateTime.new(value.to_date.year, value.to_date.month, value.to_date.mday, 15,15,15)
    logger.debug '>>>>' + start.inspect
    logger.debug '>>>>' + self.start.inspect
  end

end

# view
<%= simple_form_for(@datetimetest) do |f| %>
  <%= f.input :start_date, :as => :string %>
  <%= f.input :start %>
  <%= f.button :submit %>
<% end %>

# controller is a base scaffolding controller

In the logs i can see that start is populated:

Parameters: {..., "datetimetest"=>{"start_date"=>"03.05.2012", "start(1i)"=>"2012", "start(2i)"=>"5", "start(3i)"=>"1", "start(4i)"=>"13", "start(5i)"=>"05"}
tonymarschall
  • 3,862
  • 3
  • 29
  • 52
  • 1
    Are you *absolutely* certain that in your class instance the self.start attribute has been initialized with a value? – Dave Isaacs May 01 '12 at 12:07
  • I have the following in the log: Parameters: {..., "datetimetest"=>{"start_date"=>"03.05.2012", "start(1i)"=>"2012", "start(2i)"=>"5", "start(3i)"=>"1", "start(4i)"=>"13", "start(5i)"=>"05"} Also if add a setter method for start and do a logger.debug value.inspect i got the datetime. – tonymarschall May 01 '12 at 12:11
  • Start is a database column so this should be set through basic accessors. – tonymarschall May 01 '12 at 12:18
  • 1
    Is it possible to post more of the class in question? – Dave Isaacs May 01 '12 at 12:24
  • 1
    What rails version are you using? I've set up an app more or less the same as what you describe, and I have no problems. – Dave Isaacs May 01 '12 at 13:35
  • I am using 3.2.3. Started over again with a fresh setup and now i got the right logger output. I can also set the new start date and debug this start_date in log. But this does not override my start value. I pasted the whole code here: http://pastebin.com/wTdqPYUE – tonymarschall May 01 '12 at 14:52
  • 1
    I don't know what the problem is. I tried the self.start = DateTime.new(...) version of what you have in pastebin, and it worked fine. – Dave Isaacs May 01 '12 at 16:07
  • Strange, still not working here...In the log i got the right result, in the sql update its still the old date. – tonymarschall May 01 '12 at 18:17

1 Answers1

1

The DateTime decomposes into year, month, date of month, hour, minute, and second, but does not conveniently break into date and time.

def start_date(value)
  start = DateTime.new(value.year, value.month, value.mday,
                       start.hour, start.minute, start.second)
  logger.debug self.start.inspect
end
Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52