[LRUG] How does Rails decide how to parse ambiguous dates?

Liam McAndrew l.mcandrew at gmail.com
Tue Jul 26 09:32:58 PDT 2011


Not quite.  When your Era record is in memory it is a fully-fledged Era
object, with all of its attributes type cast, so starts_at is a Date object.

The "magic" happens in the adapter, so to/from the object to the database.

 > era.read_attribute_before_type_cast("starts_at").class
  => String

As far as I'm aware this is just basically a utility method, it does a
lookup on the attribute before it was type cast (which was earlier stored
before the type cast occurred).

I hope that helps clear it up slightly.

On Tue, Jul 26, 2011 at 5:10 PM, John Winters <john at sinodun.org.uk> wrote:

> On 26/07/11 16:56, Liam McAndrew wrote:
>
>> John,
>>
>> I believe the clue is to look here:
>> http://apidock.com/rails/**ActiveRecord/AttributeMethods/**read_attribute<http://apidock.com/rails/ActiveRecord/AttributeMethods/read_attribute>
>>
>> View the source and note the 'column.type_cast' line...
>>
>> http://apidock.com/rails/**ActiveRecord/**ConnectionAdapters/Column/**
>> type_cast<http://apidock.com/rails/ActiveRecord/ConnectionAdapters/Column/type_cast>
>>
>> Again view the source, the case statement refers to the 'type'
>> attribute, which is actually an instance variable for the
>> 'simplified_type' method...
>>
>> http://apidock.com/rails/**ActiveRecord/**ConnectionAdapters/Column/**
>> simplified_type<http://apidock.com/rails/ActiveRecord/ConnectionAdapters/Column/simplified_type>
>>
>> And that's where the magic appears to happen!
>>
>
> Hah!  So, please correct me if I'm wrong here...
>
> When my Era record is sitting in memory, it actually contains my date as a
> string, say "2011-07-26" in the format in which it would expect to write it
> to the database.  Then whenever I try to access it by any normal method,
> it's automagically converted into a Date type.
>
> The magic that's happening in this instance is actually that it's *not*
> being converted.  The reason I couldn't see where the conversion took place
> is because - there is no conversion.
>
> Have I got it now?
>
> Cheers,
> John
> ______________________________**_________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/**listinfo.cgi/chat-lrug.org<http://lists.lrug.org/listinfo.cgi/chat-lrug.org>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110726/bee95252/attachment-0003.html>


More information about the Chat mailing list