Time availability comparison, using Ruby on Rails
You don't need to have such a complex model. All you need is a table that shows the blocks of time for which the rental units are not available, in other words, a table with booked times for each rental unit. For example:
create table BOOKING
( rental_unit_id int
, start_datetime date
, end_datetime date
)
Now if you want to get a list of rental units that are available for the entirety of a given time block, say @Arrive to @Depart then all you have to do is run a query like this:select R.rental_unit_id -- (and anything else you want)
from RENTAL_UNIT R
where not exists ( select B.rental_unit_id
from BOOKING B
where B.rental_unit_id = R.rental_unit_id
and end_datetime > @Arrive
and start_datetime < @Depart )
This query says get me a list of rental units where there is no booking which overlaps the search period of interest (from @Arrive to @Depart). Note that you can play with <= @Depart depending on whether you want to have an inclusive or exclusive endpoint for your booking periods.EDIT: Handling multiple availability blocks
@OP has added a requirement for multiple availability blocks. If you are renting equipment over multiple days then @Arrive and @Depart just happen on different dates. If, as in @OP's example, the multiple days have gaps in the middle - presumably where the equipment is returned and can be rented to someone else - then you just have to add extra where not exists
clauses - one for each independent block of desired availability. Just "and" them together and you will find the rental units that are available in all of the desired time blocks. The notion of better or worse matches doesn't really apply. A rental unit is either available or it isn't.
Rails 3 - Time Comparison?
You want:
if record.updated_at < 15.minutes.ago
That's "less-than" as in "before". Trouble comparing time with RSpec
Ruby Time object maintains greater precision than the database does. When the value is read back from the database, it’s only preserved to microsecond precision, while the in-memory representation is precise to nanoseconds.
If you don't care about millisecond difference, you could do a to_s/to_i on both sides of your expectation
expect(@article.updated_at.utc.to_s).to eq(Time.now.to_s)
orexpect(@article.updated_at.utc.to_i).to eq(Time.now.to_i)
Refer to this for more information about why the times are different Compare date_time with today's date
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 %>
activerecord comparing times
@schedules = Schedule.where("team_id = ? and event = ? and time >= ?", [current_user[:team_id], "1", Time.zone.now])
The string is used directly in the SQL query so you need to make sure the column names are correct and unambiguous (if you joined on another table that also has a team_id
colum, you would need to do schedules.team_id = ? and ...
) Comparing the DateTime Objects in Ruby on Rails
Answering this question is a bit tricky without knowing the type of @model_name.schedule_time
.
However in rails ActiveSupport
provides the methods #past?
and #future?
to Date
, DateTime
and Time
instances, so that should take care of your problem:
Date.yesterday.past? => true
Time.now.end_of_day.future? => true
DateTime.now.beginning_of_day.past? => true
If @model_name.schedule_time
does not respond to these methods, then it is not one of those types, which explains that the comparison you tried did not work.
Related Topics
Rails Redirecting Invalid Route to Root
Ruby 1.9 How to Convert Array to String Without Brackets
Where to Reopen a Class in Ror
Regex That Matches Valid Ruby Local Variable Names
Reading Files in a Zip Archive, Without Unzipping The Archive
Truncate String When It Is Too Long
Ruby Rspec. Get List of All Test
How to Deploy a Threadsafe Asynchronous Rails App
Gem Install Rmagick Fails on Os X El Capitan
When Is The Enumerator::Yielder#Yield Method Useful
Sql Like Operator in Ruby on Rails
Encoding Problems in Rails on Ruby 1.9.1
Difference Between Render and Yield in Rails
Rvm - Macports Won't Update Through Proxy
Sqlite3::Sqlexception: Duplicate Column Name While Migrating
Can You Specify The Http Method to Use with Sinatra's Redirect