How do I validate if the date of an object is going on today, preventing delay timezones.
My Ruby's app hosted in the USA and my time zone is in another country.
Ex:
List of games to be played today.
<% #matches.each do |m| %>
<% if(m.date.today?) %>
<p>This match will be played today</p>
<% end %>
<% end %>
The 'if' sentence brings back the results of the matches to be played today, including upcoming games seven hours, server delay product.
Convert the date to the user's timezone.
m.date.in_time_zone(<user's time zone>).today?
Related
I want to implement a feature in Rails using the option of multiple checkbox. User must be able to choose the days in which he want the emails, like Monday, Tuesday, etc. This must be a list of checkboxes with day name as label and day index like 0 for Sunday, 1 for Monday, like that.
Also these fields must be checked when the user next time come here to edit.
I don't want to create separate db field for each day. This can be an array of day index stored in one database field. I am using Rails version 4.
Date::DAYNAMES will give the list of week days. Date::DAYNAMES.each_with_index will give the list of days with its index. I want to know the best way to implement the same.
Try this,
<%=select_tag 'days[]', options_for_select(Date::DAYNAMES.zip((0..6).to_a),
[selected days array goes here]
), :multiple => true%>
For using checkbox
<ul>
<% Date::DAYNAMES.zip((0..6).to_a).each do |day| %>
<li>
<%= check_box_tag 'days[]', day[1], [selected days array].include?(day[1]) -%>
<%= h day[0] -%>
</li>
<% end %>
</ul>
Hope it's help you.
I have a program table that belongs_to userprofile and userprofile table that has_many programs. The program has a program_year column and I wanted to display just the earliest year (value in that program_year) out of the programs if the user has multiple programs that he/she belongs to.
I have looked for similar questions and answers (How to get earliest and latest dates for associated objects). But can't seem to get my display working properly.
Having trouble understanding how to get the minimum program_year from programs for each userprofile. The code I have now..just gives me the minimum program_year of the current user and since it is in a loop. It displays the year x amount of times.
members_controller.rb
def index
#userprofiles = Userprofile.all
# the use of current_user is incorrect..
#member_program = Uprogram.where(:userprofile_id => current_user.userprofile.id).all
#member_since = #member_program.min
end
index.html.erb
<% f.uprograms.each do |program| %>
<% if program.present? %>
<!-- just want to display the lowest program_year value)
Member since: <%= program.program_year %>
<%= #member_since.program_year %>
<% end %>
<% end %>
Any insight would help! Thank you.
Couple of ideas:
1- This will get you only the earliest program_year among all the user programs:
Uprogram.where(userprofile_id: current_user.userprofile.id).order(: program_year).limit(1).pluck(:program_year).first
2- This will give you the earliest Uprogram object:
current_user.userprofile.programs.order(:program_year).limit(1).first
I am working with a Calendar and I think that calendar deals only with date without a time.
Note: requested_date is a DateTime Attribute. I want to get all reservations with requested date and covert it to date only:
users_controller.rb
def myprofile
#reservation = Reservation.all
#resv_by_date = #reservation.group_by(&:requested_date) <-- make requested_date to date only
end
myprofile.html.erb
<div id="admin">
<%= calendar do |date| %>
<%= date.day %><br>
<% #resv_by_date.each do |reservation| %>
<%= reservation.requested_date.to_date %><br>
<% end %>
<% end %>
</div>
(this month 'May') requested_date is existing on my database
image output after the solution of the 1st answer
I don't think you want to use a group on your ActiveRecord relation not group_by which is a Ruby method.
Also, the method to get a Date from a DateTime object is datetime.to_date:
#reservations_by_date = Reservation.select(:requested_date).group(:requested_date)
#reservations_by_date.each { |reservation| puts reservation.requested_date.to_date }
Normally I'd advice you to try to group the data in the DB already (using group) for performance reasons instead of ruby code (group_by). But in your specific case it seems that you indeed need to retrieve all reservations in the given time period (e.g. month) and just group them by the date so that you can display all of them in the calendar view. In that case, you indeed have to group in ruby code.
You can do that simply by updating the block in group_by:
#reservations_by_date = Reservation.all.group_by { |res| res.requested_date.to_date }
Note that most probably you'll want to narrow down the select only to the given time period (e.g. the displayed month) first, using where conditions, I skipped that part from this answer.
The grouping above will create a hash of the reservations where the keys will be the dates (not datetimes) and the values will be arrays of reservations in the given date. In the calendar view, you can simply browse once you've accessed them using the date key:
<% calendar do |date| %>
<%= date.day %><br>
<% #reservations_by_date[date].each do |reservation| %>
<%= reservation.name ... or whatever %><br>
<% end %>
I am creating a scheduling app in Rails and I am getting stuck while trying to organize the rooms by date. When I say a room, I essentially just mean a block in the schedule. A typical event might have a lunch (one room), then a networking section (another room). Here is how I am getting the rooms:
#rooms = Room.where(event_id: #current_event.id).order(:start_time)
So that returns the rooms that belong to an event, in order of the start time. In my view I loop through and display the rooms for the event, but I want to group them in the view by the date in case there is an event that is on multiple days.
Also :start_time is a datetime type in Ruby. In human speak, what I would do is look at the date portion and if the room date is not the same as the current group, I would print the new date and continue to group the rooms that fall on that day. Here is a trivial example in case I am not being clear:
Event: Staff Retreat
July 14th, 2015
-----------------------
12:30 PM
Team building Lunch Begins
------------------------
6:30 PM
Team building Dinner Begins
------------------------
July 15th, 2015
------------------------
9:30 AM
Team building Breakfast Begins
So having the grouping of rooms in the #rooms variable that is a datetime, what would be the best way to display a table like the above? I would think that in my loop I should check if the date was the same as the previous room, and if not print the date
- #rooms.each do |room|
room.start_time.strftime("%B %d, %Y")
room.start_time.strftime("%I:%M%p")
room.name
I am having trouble with the logistics because with the Model-View-Controller concept, I feel that sorting in the view may have the view do something it shouldn't have to. I am also struggling with how to do that in the view. I would think setting a variable in the controller that would hold the temporary date as I loop through would work, but it seems like that would start to get pretty messy though. What is the best way to group the various dates from the #rooms variable?
Can you try this. this will return you a hash where date is the key and value will contain all the #rooms related to that time.
#rooms = Room.where(event_id: #current_event.id)
#rooms = Hash[#rooms.sort_by{|o| o.start_time.to_date}.group_by {|room| room.start_time.to_date}.map{|l,m| [l, m.sort_by{|k| k.start_time}]}]
now you can traverse the rooms like this in the views. im putting code in erb format.
<% #rooms.each do |k, v| %>
<%= k %>
<% v.each do |room| %>
<%= room.created_at.start_time('%I:%M %p')%>
<%= #room.name or title what so ever. %>
<% end %>
<% end %>
I've been trying to get my head around this problem but even after doing lot of research I wasn't able to get a proper solution.
I'm trying to build a flight booking system. I've have an api which returns me the arrival and departure time of flights in UTC time. Now I want to show the departure and arrival time of the flight in the local time of that country. Is there any faster way to do this. So far I have got this thing working which seems quite heavy. I'm writing some sample code below.
<% flights.each do |flight| %>
<% time_zone_name = TZInfo::Country.get(flight.departure_country_code).zone_names.first %>
<% time_zone = TZInfo::Timezone.get('time_zone_name') %>
departure_time : <%= time_zone.utc_to_local(flight.departure_time) %>
<% end %>
This might help. You can get the user's time zone and Time#localtime will give you the time in the current time zone of the machine running the code
> time = Time.now.utc
=> 2013-11-11 14:42:49 UTC
> time.localtime
=> 2013-11-11 20:12:49 +0530