1

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Thalatta
  • 4,510
  • 10
  • 48
  • 79

2 Answers2

2

Use the to_date function, to convert the string to date and compare the date in model validation instead of string.

2.1.5 :006 > date = "02/03/2016"
 => "02/03/2016" 
2.1.5 :007 > changed_date = date.to_date
 => Wed, 02 Mar 2016
 
def create
  object = object_params
  object[:date] = object[:date].to_date
  
  /** code of cretae **/
end

Then use the validations. refer to this link

Sravan
  • 18,467
  • 3
  • 30
  • 54
  • but where do I convert it, if I am using strong parameters? confer: https://gist.github.com/anonymous/9f31c26f6c38fe3e21bb – Thalatta Feb 25 '16 at 05:24
  • AHHHH! https://stackoverflow.com/questions/18369592/modify-ruby-hash-in-place-rails-strong-params I think i see the light! – Thalatta Feb 25 '16 at 05:37
1

Change

combined = String(day) + "/" +String(month)+"/"+ String(year);

To

combined = new Date(year, month, day);

Rails should handle casting this format to a date.

JS Date

James Klein
  • 612
  • 4
  • 15
  • I like your approach, Unfortunately I get the same error. Because the paramaters I believe are stringified in the client/server connex: Here is what my `anticipated_arrival_date` turns into using your advice: `"Fri Apr 04 2003 00:00:00 GMT-0800 (PST)"` which looks good. Except it still, when doing the validation renders a `nil` as opposed to a ruby `date` – Thalatta Feb 25 '16 at 05:26
  • Also it looks like this is a `DateTime` in ruby lingo, and not a `Date` – Thalatta Feb 25 '16 at 05:28
  • 1
    What does `school_application_params[: anticipated_arrival_date]` output when you put a breakpoint in `def new` or whatever action this is? – James Klein Feb 25 '16 at 05:43