ActiveRecord date field

24 views
Skip to first unread message

Avery Wilkening

unread,
May 5, 2016, 11:26:19 AM5/5/16
to rubyonra...@googlegroups.com
By accident, I just realized that an ActiveRecord date field can be set
using a third way...

foo = Fo.new
foo.start_date = Date.new(2016, 1, 31)
foo.start_date = '2016-01-31'

and to my surprise...

foo.start_date = '31/1/2016'

Where can I find documentation on this? I've searched to the point of
wasting my time.

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
May 5, 2016, 12:13:32 PM5/5/16
to Ruby on Rails: Talk


On Thursday, May 5, 2016 at 4:26:19 PM UTC+1, Ruby-Forum.com User wrote:
By accident, I just realized that an ActiveRecord date field can be set
using a third way...

foo = Fo.new
foo.start_date = Date.new(2016, 1, 31)
foo.start_date = '2016-01-31'

and to my surprise...

foo.start_date = '31/1/2016'

Where can I find documentation on this? I've searched to the point of
wasting my time.


In the most general case, active record ends up calling Date._parse ( http://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/Date.html#method-c-_parse ) from the ruby stdlib which tries to guess/handle all sorts of different formats  ( https://github.com/ruby/ruby/blob/trunk/ext/date/date_parse.c ) but without documenting what they are (as far as I can see)

Fred 

botp

unread,
May 5, 2016, 12:59:15 PM5/5/16
to rubyonra...@googlegroups.com
On Thu, May 5, 2016 at 11:25 PM, Avery Wilkening <li...@ruby-forum.com> wrote:
> Where can I find documentation on this?

there are lots, try this eg
https://robots.thoughtbot.com/how-rails-works-type-casting
just remember, this is ruby, and "foo=" can be a method call.

kind regards --botp
Reply all
Reply to author
Forward
0 new messages