Active Record - Select by multiple days of week

Wondering what would be the best way to implement this.

I need to store that an item occurs on a specific day of the week or
everyday.
Say in the model we have a title and a day of week.
I was thinking doing a 0-7 in my model for the item representing the
day of the week with zero being everyday.

For a selection criteria say I wanted to search for every title
starting on Monday, Wendsday and Friday where each day on the
selection screen would be checkbox representing the days Monday -
Sunday and Everyday (clicking everyday would remove the checkboxes
should someone select everyday and a day of the week)

What would the active record query look like to dynamically pass in
the days of the week selected should say mon, wed and fri get selected
given that what they selected is past to the controller in a
“days_of_week” variable.

I’m not totally tied to the idea of storing the day as a number so if
anyone has any better implementations of this I’m listening.

Cheers,
S Gallo

On 23 February 2010 21:33, sgallo1 [email protected] wrote:

Wondering what would be the best way to implement this.

I need to store that an item occurs on a specific day of the week or
everyday.
Say in the model we have a title and a day of week.
I was thinking doing a 0-7 in my model for the item representing the
day of the week with zero being everyday.

You could store an seven-figure number and bitwise it to work out
whether a given day was on or off.

Or you could have a separate table for “days” and have a many_to_many
association with it, to get a “days” collection in your model.

Or you could use a recurrence rule, if you need more flexibility
(something like RiCal)

Any preference?

Michael

PS Using “zero” for “all” would not be very intuitive, as 0 is
normally used to indicate Sunday.

On Tue, Feb 23, 2010 at 4:33 PM, sgallo1 [email protected] wrote:

selection screen would be checkbox representing the days Monday -
Sunday and Everyday (clicking everyday would remove the checkboxes
should someone select everyday and a day of the week)

What would the active record query look like to dynamically pass in
the days of the week selected should say mon, wed and fri get selected
given that what they selected is past to the controller in a
“days_of_week” variable.

I’m not totally tied to the idea of storing the day as a number so if
anyone has any better implementations of this I’m listening.

Something like

get weekdays_selected to be an array of the numbers corresponding to

the checked boxes plus 0

For example if you wanted to find events on Mondays and Wednesdays,

weekdays_selected should be:

[0, 2, 3]

Items.all(:conditions => [“weekday IN ?”, weekdays_selected])

or if you’re using Rails3

Items.where(“weekday IN ?”, weekdays_selected)

If it were me I might think about tweaking this a bit and use 7
instead of 0 for any day, since Ruby’s Date and Time use 0 for Sunday
through 6 for Saturday.

Caveat, none of the above code has been tested, and I’ve only had two
sips of coffee this morning.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Thanks all. I will replace 7 with everyday and 0 for sunday.
And Rick thanks for the active record code. That’s exactly what I was
looking for.

Cheers,
S Gallo

On Wed, Feb 24, 2010 at 8:14 AM, Rick DeNatale [email protected]
wrote:

starting on Monday, Wendsday and Friday where each day on the
anyone has any better implementations of this I’m listening.
Items.all(:conditions => [“weekday IN ?”, weekdays_selected])
sips of coffee this morning.
You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Stephen Gallo
[email protected]
843.424.1933

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs