Rails 3 Time Comparison - ruby-on-rails

I have my movies page and I'm trying to make the movie status automatically change depending on the release_date
I have this so far, but it doesnt seem to work
<% if #movie.release_date.strftime("%d %B %Y") > Time.now.strftime("%d %B %Y") %>
Released
<% else if #movie.release_date.strftime("%d %B %Y") < Time.now.strftime("%d %B %Y") %>
Upcoming
<% end %>
<% end %>
How can i make it so that if the release date I put in the form (day month year) is before the current date (day month year), it puts "Released" and then vice versa for "Upcoming" so if the release date is after the current date (day month year)

<% if #movie.release_date.past? %>
Released
<% else %>
Upcoming
<% end %>
Don't use string for comparison, compare the original object.

Related

How to sort group_by month and year in view?

pages_controller.rb
#past_challenges_by_years = #past_challenges.group_by { |t| [t.deadline.year, t.deadline.month] }
How can I break it down in the view with year and then subdivide it with month like this:
2014 # Years
01 # Months
Challenge
Challenge
02
Challenge
08
Challenge
2016
03
Challenge
08
Challenge
view.html.erb
<% #past_challenges_by_years.sort.each do |year, challenges| %>
<%= year %>
<%= month %> # I don't know how to define this.
<% for challenge in challenges %>
etc...
<% end %>
<% end %>
I would start by sorting the keys and then look up the challenges while iterating through those keys.
<% #past_challenges_by_years.keys.sort.each do |(year, month)| %>
<%= year %>
<br />
<%= month %>
<br />
<% #past_challenges_by_years[[year, month]].each do |challenge| %>
<%= challenge %>
<br />
Edit: Here is a new solution that only displays years and months once.
#past_challenges_by_years.keys.map { |a| a[0] }.uniq.sort.each do |year|
puts year
#past_challenges_by_years.keys.select { |a| a[0] == year }.map { |a| a[1] }.uniq.sort.each do |month|
puts month
#past_challenges_by_years[[year, month]].each do |challenge|
puts challenge
end
end
end
I think it should be simple enough to convert to ERB.

Compare date_time with today's date

I’m trying to compare a saved date_field with today’s date. Seems to work in console but I can't make it work in my code.
<% #flights.each do |flight| %>
<% if (Time.new("%Y-%m-%d") < flight.flight_date) %>
<tr class="active">
<% else %>
<tr class="success">
<% end %>
Thanks to #drenmi for the patient answers and explaining why that my date_field was saved as a datetime attribute hence Time.now working and not Date.today.
Now to fix my naming so I follow convention!
Using Time.new("%Y-%m-%d") will produce an empty Time object:
Time.new("%Y-%m-%d")
# => 0000-01-01 00:00:00 +0730
What you want to do is compare to Time.now:
<% if Time.now < flight.flight_date %>
Or, if using Rails, you might want to use Time.current, or Date.current:
<% if Date.current < flight.flight_date %>
or
<% if Time.current < flight.flight_date %>
you want using Date.today in rails
Time.now.utc
this is the best choice
time = Time.now.utc.iso8601
Time.iso8601(time)
...
don't forget: require 'time'

Ruby on Rails only show data value once if there are more than one of the same

In my ruby on rails application I am currently displaying the date and time each film is shown through:
<% if not #film.showings.blank? %>
To book click on a time below:</br>
<% #film.showings.each do |showing| %>
<%= showing.show_date.strftime("%A %e %B %Y") %># <%= showing.show_time.strftime("%H:%M") %><br>
<% end %>
<% else %>
<p>There are currently no showings for this film.</p>
<% end %>
And this displays data like the following:
Sunday 25 January 2015 # 12:00
Sunday 25 January 2015 # 16:00
Monday 26 January 2015 # 11:00
Monday 26 January 2015 # 22:00
Tuesday 27 January 2015 # 22:00
Wednesday 28 January 2015 # 11:00
Wednesday 28 January 2015 # 12:00
Wednesday 28 January 2015 # 16:00
Wednesday 28 January 2015 # 19:30
But what I want to be able to do is if the date is repeated then it only shows it once and repeats the time, so for example the date Sunday 25 January 2015 would be shown as:
Sunday 25 January 2015 # 12:00 16:00
You should group records by date and join hours.
<% if not #film.showings.blank? %>
To book click on a time below:</br>
<% #film.showings.group_by{|showing| showing.show_date.strftime("%A %e %B %Y") }.to_a.each do |showing| %>
<%= showing.first %># <%= showing.last.map{|s| s.show_time.strftime("%H:%M")}.join(' ') %><br>
<% end %>
<% else %>
<p>There are currently no showings for this film.</p>
<% end %>
You could place a function like this in helpers or a decorator if you are using them. I'm assuming you pass in the showings from #file.showings. This will return an array that is how you want that you could loop over in the view and display.
def show_times(showings)
showings.each_with_object({}) do |showing, hash|
key = showing.show_date.strftime("%A %e %B %Y")
hash[key] ||= []
hash[key] << showing.show_time.strftime("%H:%M")
end.map do |date, times|
"#{date} # #{times.join(' ')}"
end
end
Given an array of datetime objects, you can use the following code to extract a nested hash of date and time showings. It probably best lives inside your Film model.
def showings_hash
showings.pluck(:show_time).map {|datetime| datetime.to_date}.uniq.map{|date| {date => dates.select{|datetime| date == datetime.to_date}}}
end
This returns:
- 2015-01-12:
- !ruby/object:DateTime 2015-01-12 18:00:00.000000000 Z
- !ruby/object:DateTime 2015-01-12 19:00:00.000000000 Z
- !ruby/object:DateTime 2015-01-12 20:00:00.000000000 Z
- 2015-01-13:
- !ruby/object:DateTime 2015-01-13 20:00:00.000000000 Z
You can then iterate through each showing date in the view like so:
<% #film.showings_hash.each do |showing_date| %>
<li>
<%= showing_date %>
<% showing_date.each do |showtime| %>
<%= showtime.strftime("%H:%M") %>
<% end %>
</li>
<% end %>

Rails get date to variable

I would like to make something like show posts from a particular date.
I have
<% date =Date.parse('2014-06-18') %>
<% if feed_item_users.created_at < (date+1).strftime('%Y-%m-%d') && feed_item_users.created_at > date %>
<li id="<%= feed_item_users.id %>">
<span class="content"><%= wrap(feed_item_users.content) %></span>
<span class="timestamp">
Published: <%= I18n.l feed_item_users.created_at, :format => :h %>
</span>
</li>
<% end %>
in my view I'm checking date.
Now I would like to make a date_time select to get date from my web in format like '2014-06-18' and make default today date.
I know that it should look like that:
<%= form_for(??????) do |f| %>
<div class="field">
<%= f.label :published_at %><br>
<%= f.datetime_select :published_at %>
</div>
<% end %>
but I don't know how to make it to variable because I need it only one time.
You're comparing dates against strings here which isn't reliable. In your ruby code you should always compare dates against dates (or times).
Your logic seems to be "if created at is greater than the start of today and less than the start of tomorrow", which can be massively simplified to "if it's today".
ie you can replace this
<% date =Date.parse('2014-06-18') %>
<% if feed_item_users.created_at < (date+1).strftime('%Y-%m-%d') && feed_item_users.created_at > date %>
with this
<% if feed_item_users.created_at.to_date == Date.today %>
The datetime_select is documented here
http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
strftime returns string so when you apply less then operator it doesn't work.
Better way is to do it like this:
time = Time.new(2014, 06, 18)
if feed_item_users.created_at > time.at_beginning_of_day && feed_item_users.created_at < (time+1.day).at_beginning_of_day

Time on Rails baseball schedule app off by 5 hours on production

I have an app: http://arethebaronsplaying.com/ that breaks everyday at 7pm US Central time on production, but works correctly locally. The site is hosted by Ninefold.
In short, I have a seeds.rb file with a bunch of Game objects, and one of the attributes for a Game is date, which is set to the m/d format, ex. 4/09.
And every day at 7pm the app breaks by displaying a big NO and text saying when the next game is, and the next game is the current day's game. Then below that, it will display a YES! with the current day's game.
So it's displaying a YES and a NO, which makes me think that perhaps Date.today is calculating time differently than Time.now, and since I'm using both of them, the one that is wrong will display the NO.
Anyway, here's my logic:
<% i = 0 %>
<% games.each do |game| %>
<% if game.date.strftime("%_m/%d")[1..-1] == Time.now.strftime("%_m/%d")[1..-1] && game.away == false %>
<h1 class="main-text answer yesanswer" id="responsive_headline"><%=link_to "YES!", "http://www.milb.com/tickets/singlegame.jsp?sid=t247", target: "_blank" %></h1>
<% i = 1 %>
<br>
<h2 class="main-text2 gamewrap" id="responsive_headline2">
<% if game.away == false %>
<span class="next-venue">vs.</span>
<span class="next-opponent"><%= game.opponent %></span> |
<span class="next-time"><%= game.time %></span>
<% else %>
<span class="next-venue">at</span>
<span class="next-opponent"><%= game.opponent %></span> |
<span class="next-time"><%= game.time %></span>
<% end %>
</h2>
<% elsif game.date.strftime("%_m/%d")[1..-1] == Time.now.strftime("%_m/%d")[1..-1] && game.away == true %>
<% unless i == 1 %>
<a><h1 class="main-text answer" data-reveal-id="myModal" data-reveal id="responsive_headline">NO.</h1></a>
<% i = 1 %>
<h2 class="main-text2 gamewrap" id="responsive_headline2">
<span class="next-venue">away game</span>
<span class="next-opponent">#<%= game.opponent %></span>
</h2>
<% end %>
<% else %>
<% unless i == 1 %>
<a><h1 class="main-text answer" data-reveal-id="myModal" data-reveal id="responsive_headline">NO.</h1></a>
<% i = 1 %>
<h2 class="main-text2 gamewrap" id="responsive_headline2">
<span class="next-venue">vs.</span>
<span class="next-opponent">
<% if next_home_game > 1 %>
<%= game.opponent %>
in <%= next_home_game %> days |
<% else %>
<%= game.opponent %>
in <%= next_home_game %> day |
<% end %>
<span class="next-time"><%= game.time %></span>
</h2>
<% end %>
<% end %>
<% end %>
I've tried adding config.time_zone = 'Central Time (US & Canada)' to both application.rb and production.rb and it broke the deployment.
Here's how Ninefold does Time: https://help.ninefold.com/hc/en-us/articles/201320124-What-time-standard-does-Ninefold-use-
Thanks for your help!
Dates and times (across timezones) are a nightmare to manage (I know that's not much help... but I've been there and you have my condolences).
Anyway... try replacing all of your Time.now and Date.today calls with Time.zone.now and Time.zone.today respectively.
Ensure all of your times are stored in the DB in UTC (no matter what your local time, or the server's time) and check out some articles that give other advice (like http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails and http://danilenko.org/2012/7/6/rails_timezones/)
Good luck!

Resources