I have values in table for duties assigned on certain days ranges, e.g
Monday to Tuesday, Wednesday to Friday.
Now I want to show the duties assigned to a person on main screen if the
day of current date is in the range of weekdays assigned to him/her.
I tried the below but no idea.
In order to write s/w you need to be able to debug your code to see
why it is not working. An easy way in rails is just to insert code to
log results to log/development.log, so you could insert into your code
something like:
Employee_id
duty_name
duty_day1 //start day
duty_day2 //end day
duty_time
What is saved in duty_dayn? Day as string or as number (0..7)?
Its, Monday, Wednesday etc. as string
I suggest not doing that, store it as a number and convert when you
want to display or enter it. That will make querying the database
much simpler. Then you can get all the duties for an employee for
today by something like
In fact it may not be quite that simple. It will not cope with a duty
running from Saturday to Sunday for example, so you may have to extend
it for that, possibly a where clause something like
"(duty_day1 <= duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or
(duty_day1 > duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",
today, today, today, today
I have not tested that so convince yourself it is right before trying
it. Don't forget to provide automated tests to check all the edge
conditions.
"(duty_day1 <= duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or
(duty_day1 > duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",
today, today, today, today
I have not tested that so convince yourself it is right before trying
it. Don't forget to provide automated tests to check all the edge
conditions.
One thing to watch out for is time zones. If the user and the rails
server are both set to the same timezone then you should be ok, but if
they are using different timezones then time.now (which runs on the
server) will not show the users time, so the time (for the user) at
which the current day changes from, for example, sunday to monday,
will not be midnight.