ActiveRecord simple question about date

23 views
Skip to first unread message

rusik

unread,
Nov 19, 2013, 2:07:11 PM11/19/13
to rubyonra...@googlegroups.com
Hi all, i have User model with bdate: string
Try : 
User.find(2).bdate
=> Wed, 09 May 1945
User.find(2).bdate.to_s
=> "1945-05-09"
But if i run:
User.where(bdate: "1945-05-09")
=> []
Why???

Colin Law

unread,
Nov 19, 2013, 3:09:01 PM11/19/13
to rubyonra...@googlegroups.com
On 19 November 2013 14:07, rusik <rus...@gmail.com> wrote:
> Hi all, i have User model with bdate: string
> Try :
>>
>> User.find(2).bdate
>> => Wed, 09 May 1945
>> User.find(2).bdate.to_s
>> => "1945-05-09"

Are you sure bdate is of type string in the database? If so then why
does it appear to interpret it as a date in your first example?

Colin

>
> But if i run:
>>
>> User.where(bdate: "1945-05-09")
>> => []
>
> Why???
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/2989915a-d24e-491f-87e2-efb061910ff2%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

rusik

unread,
Nov 19, 2013, 6:36:29 PM11/19/13
to rubyonra...@googlegroups.com, cla...@googlemail.com
yes, it is string. I find decision , i am using date without zero's like this:
User.where(bdate: "1945-5-9") 
вторник, 19 ноября 2013 г., 19:09:01 UTC+4 пользователь Colin Law написал:

Hassan Schroeder

unread,
Nov 19, 2013, 7:25:37 PM11/19/13
to rubyonrails-talk
On Tue, Nov 19, 2013 at 10:36 AM, rusik <rus...@gmail.com> wrote:
> yes, it is string.

Uh, well.

If the datatype of that column were String, would you think you'd
be likely to get different results from bdate and bdate.to_s?

>> >> User.find(2).bdate
>> >> => Wed, 09 May 1945
>> >> User.find(2).bdate.to_s
>> >> => "1945-05-09"

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Colin Law

unread,
Nov 19, 2013, 8:50:11 PM11/19/13
to rubyonra...@googlegroups.com
On 19 November 2013 18:36, rusik <rus...@gmail.com> wrote:
> yes, it is string. I find decision , i am using date without zero's like
> this:

Just to convince us show us the users table entry from db/schema.rb.

Colin

Robert Walker

unread,
Nov 19, 2013, 10:22:40 PM11/19/13
to rubyonra...@googlegroups.com
Colin Law wrote in post #1127964:
> On 19 November 2013 18:36, rusik <rus...@gmail.com> wrote:
>> yes, it is string. I find decision , i am using date without zero's like
>> this:
>
> Just to convince us show us the users table entry from db/schema.rb.

Just a thought, but if the birthday is truly being stored in a string
(varchar) field, doesn't that make localizing your application somewhat
more difficult?

05/09/1945 - U.S.A
09.05.1945 - Europe
1945年5月9日 - Japan
Wednesday, May 9, 1945 - U.S.A Long Format

--
Posted via http://www.ruby-forum.com/.
Reply all
Reply to author
Forward
0 new messages