Dear Friends,
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.
def show_all_rosters
@dw = Time.now.strftime(’%w’)
day = Proc.new { |d| Date::DAYNAMES[d] }
@dw1 = day.call(6)
week_days = [“sunday”, “monday”, “tuesday”, “wednesday”, “thursday”,
“friday”, “saturday”]
@ww = week_days[1]
@dd1 = Date.today
@dd2 = Date.tomorrow
@dd3 = Date.today + 1
@theday =Time.now.strftime(’%w’).downcase
@rosterduties = RosterDuty.find(:all,:conditions=>[“lower(duty_day1) =
:theday or lower(duty_day2)=:theday”,{:theday => @theday}])
end
Thanks in advance.
On 13 March 2016 at 09:53, Naveed A. [email protected] wrote:
Dear Friends,
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:
def show_all_rosters
@dw = Time.now.strftime(‘%w’)
logger.info “@dw: #{@dw.inspect}”
day = Proc.new { |d| Date::DAYNAMES[d] }
logger.info “day: #{day}”
and so on to see what is not working and fix it. If you don’t do that
then you will never know why the code is not doing what you want.
After you have done that and either got it working or hit a brick wall
then come back and ask if there is a better way.
Colin
and so on to see what is not working and fix it. If you don’t do that
then you will never know why the code is not doing what you want.
After you have done that and either got it working or hit a brick wall
then come back and ask if there is a better way.
Colin
Sorry for not making you understand the problem due to my English skill,
dear all these lines works, but I dont know how to do the following:
e.g A person is assigned to work from Monday to Wednesday of every month
and its Tuesday suppose or Monday or Wed, Computer should show that He
has to do the work today.
hope u will get it now.
On 13 March 2016 at 10:49, Naveed A. [email protected] wrote:
e.g A person is assigned to work from Monday to Wednesday of every month
and its Tuesday suppose or Monday or Wed, Computer should show that He
has to do the work today.
hope u will get it now.
What are you storing in the database to indicate the working days?
Tell us the field names and what the fields contain.
Colin
On 13 March 2016 at 12:07, Colin L. [email protected] wrote:
dear all these lines works, but I dont know how to do the following:
Tell us the field names and what the fields contain.
Also how do you record which RosterDuty records relate to a particular
person?
Colin
Also how do you record which RosterDuty records relate to a particular
person?
Colin
My field names are as below:
id
Employee_id
duty_name
duty_day1 //start day
duty_day2 //end day
duty_time
On 13 Mar 2016 14:06, “Naveed A.” [email protected] wrote:
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)?
–
Posted via http://www.ruby-forum.com/.
–
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/cb6dfa79c02e68303e0d0a89e9d6afcb%40ruby-forum.com
.
Colin L. wrote in post #1182172:
On 13 Mar 2016 14:06, “Naveed A.” [email protected] wrote:
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
“(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.
Is it something like that you are looking for?
Colin
let me try this then will tell u what happened?
On 13 March 2016 at 14:46, Naveed A. [email protected] wrote:
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
today = Time.now.wday
@employee.roster_duties.where( “duty_day1 >= ? and duty_day2<=?”, today,
today)
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.
Is it something like that you are looking for?
Colin
Colin
Dear I changed the Days from names to numbers, in my Add_rosters its
like below now:
<%= a.select :duty_day1, [[ “Monday”,“1”],
[“Tuesday”,“2”],[“Wednesday”,“3”],[“Thursday”,“4”],[“Friday”,“5”],[“Saturday”,“6”],[“Sunday”,“0”]],:selected
=> [“Monday”,“1”] %>
Change the controller’s method to this:
def show_all_rosters
@theday =Time.now.strftime(’%A’).downcase
@rosterduties = RosterDuty.find(:all,:conditions=>[“duty_day1 >= :theday
and duty_day2 <=:theday”,{:theday => @theday}])
end
and show_all_rosters view is this:
<% if @rosterduties.present? %>
<% @rosterduties.each do |r| %>
<%= r.duty_name %>
<%= r.duty_place %>
<%= r.duty_time %>
<%= r.duty_day1 %>
<%= r.duty_day2 %>
<%end%>
<%end%>
But the result is blank.
On 14 March 2016 at 11:56, Naveed A. [email protected] wrote:
Change the controller’s method to this:
def show_all_rosters
@theday =Time.now.strftime(‘%A’).downcase
Have you tried inserting logger.info lines as I suggested previously
to see what is happening. What is the value of @theday
@rosterduties = RosterDuty.find(:all,:conditions=>[“duty_day1 >= :theday
and duty_day2 <=:theday”,{:theday => @theday}])
Why are you not using Roster.Duty.where(…)?
Colin
Why are you not using Roster.Duty.where(…)?
Colin
Thanks dear it worked, I just changed
@theday =Time.now.strftime(’%A’).downcase
to this
@theday =Time.now.strftime(’%w’)
and the condition line as you suggested to this
@rosterduties = RosterDuty.find(:all, :conditions => ["(duty_day1 <=
duty_day2 and duty_day1 <= ? and duty_day2 >= ?) or (duty_day1 >
duty_day2 and (duty_day1 <= ? or duty_day2 >= ?))",@theday, @theday,
@theday, @theday])
thanks again.
On 16 March 2016 at 08:05, Naveed A. [email protected] wrote:
@theday =Time.now.strftime(‘%w’)
Does Time.now.wday not work?
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.
Colin