Topic: Find all records for a given month

I have a WorkOrder model with a "start" attribute (date column). The start column holds the date that the work order was started.

My route looks like "/calendar/:year/:month"

So, given 'params[:year] = 2006' and 'params[:month] = 12', what would be a clean set of conditions to find all the work orders within that month?

WorkOrder.find(:all, :conditions => ['start ... ?', ...])

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Find all records for a given month

Try:

time = Time.new(params[:year], params[:month])
WorkOrder.find(:all, :conditions => ['start >= ? AND start <= ?', time.beginning_of_month, time.end_of_month])

Or this might work too:

WorkOrder.find(:all, :conditions => ['YEAR(start) = ? AND MONTH(start) = ?', params[:year], params[:month]])

Railscasts - Free Ruby on Rails Screencasts

Re: Find all records for a given month

Could aloso use BETWEEN for mysql

SELECT * from foo where date BETWEEN '2006-01-01' AND '2006-01-31';

Last edited by Catharsis (2006-12-14 09:17:51)

Re: Find all records for a given month

I'd go for the year() and month() sql functions myself.

vinnie - rails forum admin

Re: Find all records for a given month

err just a illuminated a cool way to generate the between statment:

>> (7.days.ago..1.day.ago).to_s(:db)
=> "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'"

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?

Re: Find all records for a given month

Wow, I never knew ranges implemented to_s(:db). What a great find!

Railscasts - Free Ruby on Rails Screencasts

Re: Find all records for a given month

No kidding - that's incredible.

Ok, now try to imagine that kind of helper existing in PHP.  Anybody?  Yeah.  Not a chance.

Re: Find all records for a given month

jed.hurt wrote:

err just a illuminated a cool way to generate the between statment:

>> (7.days.ago..1.day.ago).to_s(:db)
=> "BETWEEN '2006-12-11 02:06:50' AND '2006-12-17 02:06:50'"

That's an awesome blog entry, lots of great stuff in there.

I never knew about

>> 5.ordinalize
=> "5th"

awesome.

Re: Find all records for a given month

tortoise wrote:

That's an awesome blog entry, lots of great stuff in there.

I never knew about

>> 5.ordinalize
=> "5th"

awesome.

yep, err has to be about the best in the rails blogosphere

Last edited by jed.hurt (2006-12-19 21:01:21)

I thought about how mothers feed their babies with tiny little spoons and forks, so I wondered what do Chinese mothers use. Toothpicks?