Home > Ruby on Rails > has_many’s build method and dates

has_many’s build method and dates

Let’s say you have a HTML form with a date field, and after submitting it, you will be passing the parameters into your ActiveRecord class’ build method (made available by the inclusion of :has_many) to create an instance of the class.

If your date does not have the “day” component, then the build will return an instance of the class with the date variable as a nil.

Therefore, make sure that your date has a “day” component if you are using a text input field for your date in the HTML form.  Or rather, make sure that your date has all the components.

If you are using a date_select with only the month and year select lists in the HTML form, you are still safe, because Rails will still have the day component (set to today) included in the request’s parameters list.

I am sorry if none of this makes sense to you. But it will if you have been trying to make your dates work with the model class’ build method.

Just a last, and hopefully understandable statement, if you are using the build method for your model class, and there is a datetime variable, then make sure that the date is complete with day, month and year. Otherwise you’ll get a nil for the datetime variable in the class returned.

Categories: Ruby on Rails
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: