Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating one datetime to be later than another
    primarykey
    data
    text
    <p>When I try and validate two Datetime attributes in their model, I get the following:</p> <blockquote> <p>undefined method `to_datetime' for nil:NilClass</p> </blockquote> <p>It highlights my function</p> <pre><code>def validate_timings if (startDate &gt; endDate) errors[:base] &lt;&lt; "Start Time must be less than End Time" end end </code></pre> <p>and in particular the use of '>'</p> <p>I think it may be the way I'm handleing the dates, but I'm not sure. This is how the dates are passed: </p> <pre><code>"startDate(1i)"=&gt;"2013", "startDate(2i)"=&gt;"12", "startDate(3i)"=&gt;"18", "startDate(4i)"=&gt;"10", "startDate(5i)"=&gt;"24", "endDate(1i)"=&gt;"2013", "endDate(2i)"=&gt;"12", "endDate(3i)"=&gt;"18", "endDate(4i)"=&gt;"11", "endDate(5i)"=&gt;"24", </code></pre> <p>P.S I know my naming conventions are incorrect, I will change them in the next migration.</p> <p>UPDATE: Here is my full Model</p> <pre><code> class Appointment &lt; ActiveRecord::Base belongs_to :car belongs_to :service accepts_nested_attributes_for :service accepts_nested_attributes_for :car attr_accessor :period, :frequency, :commit_button validates_presence_of :car_id, :service_id, :startDate, :endDate validate :validate_timings def validate_timings p startDate, endDate if (startDate &gt; endDate) errors[:base] &lt;&lt; "Start Time must be less than End Time" end end def update_appointments(appointments, appointment) appointments.each do |e| begin st, et = e.startDate, e.endDate e.attributes = appointment nst = DateTime.parse("#{e.startDate.hour}:#{e.startDate.min}:#{e.startDate.sec}, #{st.day}-#{st.month}-#{st.year}") net = DateTime.parse("#{e.end.hour}:#{e.end.min}:#{e.end.sec}, #{et.day}-#{et.month}-#{et.year}") #puts "#{nst} ::::::::: #{net}" rescue nst = net = nil end if nst and net # e.attributes = appointment e.startDate, e.endDate = nst, net e.save end end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload