gen
gen

Reputation: 21

How to implement search days of week

How to search by days of week in rails? For example: I need to create object that's available every week on Monday and Friday or on period from Monday to Thursday. when i make a get request to my search controller with 2 data values and get a response with available objects. Right now i implement it in much more simple way , my model have 2 columns : datestart and dataend and my search controller search between 2 dates. How should i upgrade my models and controller for search days on every week

Upvotes: 2

Views: 60

Answers (1)

Pierre Michard
Pierre Michard

Reputation: 1391

this seems hard to do in a database agnostic way. You will probably have to adapt your query to the database you're using for searching events happening a specific day in the week. For example if you use postgresql you could use the following scope (dow [0-6])

scope :this_day( day), -> { where("date_part("dow", datestart) >= ?", day).
                       where('date_part("dow", dateend) <= ?', day)

If you use sqlite, you could use the strftime function to do the same task

Upvotes: 1

Related Questions