This may be impossible to do, in this manner. But, in my db schema, I have a field called anticipated_arrival_date
which is of type t.date
in my schema.rb
file.
I am currently using jQuery to grab three disparate <select>
strings, and concatenate them into a string, representing a date in the following format "dd/mm/yyyy"
. I do this in the following way:
$("#arrival_year").blur(function(){
var month = $('#arrival_month').val();
var day = $('#arrival_day').val();
var year = $('#arrival_year').val();
if (month == "" || day=="" || year==""){
}
else{
//DD/MM/YYYY
combined = String(day) + "/" +String(month)+"/"+ String(year);
var arrival_date = $('#school_application_anticipated_arrival_date');
arrival_date.val(combined);
}
});
where #school_application_anctipated_arrival_date
is the t.date
field on my model which I have made into a f.hidden_field
in my <form>
. The problem is that when I submit the form via POST request, I get the following error:
undefined method `<=' for nil:NilClass
if record.anticipated_arrival_date != nil and ((record.start_date <= record.anticipated_arrival_date) or ((record.start_date - record.anticipated_arrival_date).to_i >= 7))
which is thrown on my ancitipated_arrival_date
validation. in short it is saying that anticipated_arrival_date
is undefined.
Is it A) somehow possible to make a valid t.date
from a string when create
is invoked? If not, is there a way, assuming a migrate anticipated_arrival_date
to a string as opposed to a date
, that I can make a safe comparison in a validation from anticipated_arrival_date
to another column which is a real date
column? For example using strftime
on the date
column?
Here is my complete controller file:
https://gist.github.com/anonymous/9f31c26f6c38fe3e21bb
And here is the excerpt of the model file which performs the validations:
class AnticipatedArrivalDateValidator < ActiveModel::Validator
def validate(record)
if record.anticipated_arrival_date != nil and ((record.start_date <= record.anticipated_arrival_date) or ((record.start_date - record.anticipated_arrival_date).to_i >= 7))
record.errors[:base] << "Cannot have arrival date be later or equal to start date for Early Night Arrival."
end
end
end
NOTE: In Reference to comments, I have tried converting the param into a Date via the following before_action
before_action :cleanup_date_params
def cleanup_date_params
if params[:school_application]
anticipated_string = params[:school_application][:anticipated_arrival_date]
params[:school_application][:anticipated_arrival_date] = Date.parse(anticipated_string)
Rails.logger.debug(Date.parse(anticipated_string))
end
end
the problem is that this still results in the error:
undefined method `<=' for nil:NilClass
called on the same line in my Validation.
I think that, the before_action
is not changing params[:school_application][:anticipated_arrival_date]
because the .create
is called on the strong parameter version of params[:school_application]
and thus must overwrite the params I set in before_action
. I don't know how to remedy this.