Forum: Ruby on Rails extract month from date column

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
05337aed68267daec2996e5ca732d476?d=identicon&s=25 Akbar (Guest)
on 2006-02-20 12:00
(Received via mailing list)
Hi,

I have this rails query:
    @birthdays = Birthday.find :all,
                               :conditions => ["user_id = ? and
birthdate = '2006-02-01', @user_id],
                               :limit  =>
@birthday_pages.items_per_page,
                               :offset =>
@birthday_pages.current.offset

You can ignore the user_id, limit and offset part. Birthdate is date
type. That query lists the row which birthdate is 2006-02-01. How do I
extract month from birthdate column; something like this:
.... birthdate.month = 2 .....
I want to query all the rows which in February or another month.

I have though the solution; something like this:
.... birthdate >= start_of_the_month and birthdate <= end_of_the_month
.... ( we find start and end of the month from ruby )
but maybe rails has simpler way....

Thank you.
F59329dc91cba06600ff65c85fd3e93c?d=identicon&s=25 Anthony Green (acgreen)
on 2006-02-20 13:23
 How do I
> extract month from birthdate column; something like this:
> .... birthdate.month = 2 .....

I couldn't find anything at http://api.rubyonrails.com/

You could try extending with a method thats build some custom SQL

WHERE MONTH(birthdate);

_T
This topic is locked and can not be replied to.