I have an input for time, and I only want to accept a couple of input values (6am 7am 8am 9am 10am 11am 12pm ...).
In my model, I have tried:
VALID_TIMES = %w(6am 7am 8am 9am 10am 11am 12pm 1pm 2pm 3pm 4pm 5pm 6pm 7pm 8pm 9pm 10pm)
validates_inclusion_of :final_selected_time, :in => VALID_TIMES
but it isn't working. I have also tried other methods according to the rails form validation documentation, but none of them seem to work with strings. They only work with numbers.
Does anyone know a way to validate a param like :final_selected_time for string values?
Update:
The above code should work to validate strings.
You should provide us with more information. We cant help you without it. What does object actually get? Your code is ok since you changed column type to string. It checks if value equals to one of strings you specified.
Anyway you can always make the validation yourself without rails DSL (almost):
validate :final_selected_time_should_be_NICE
private
def final_selected_time_should_be_NICE
return if final_selected_time ... # is ok
errors.add(:final_selected_time, :not_appropriate)
end
Just change method name so it will correspond to your business logic and add condition.
Related
I created a database using rails with stime attribute (referring to the starting time). Its type is Time. When I added a record with:
stime = "10:10"
it has been added as:
"stime":"2000-0101T10:10:00.000Z"
I only want the time, not the date. I thought it's the difference between Time and DateTime. Could anyone tell me what the problem is?
As the documentation states, it is not.
Time is an abstraction of dates and times. Time is stored internally
as the number of seconds with fraction since the Epoch, January 1,
1970 00:00 UTC.
There are several things you can do:
Store it as a string and do regex validation.
If you are using Postgres and don't mind depending on it, you can use time type. This would allow you do things like: YourModel.where("stime > '13:00:00'").
Store the time of the day in seconds since midnight. This will require you to add some helper methods for creating and scopes for querying data. You might find ActiveSupport's extension on numeric values helpful.
One way of handling this issue, to exemplify Uzbekjon's third point, is storing the time as seconds since midnight in an integer column:
stime = Time.now
# => 2016-04-13 10:58:13 -0700
seconds = stime.seconds_since_midnight.to_i
# => 39493
You can then retrieve this value and parse it into the time of day when needed:
time = Time.at(seconds).utc
# => 1970-01-01 10:58:13 UTC
result = time.strftime("%I:%M")
# => "10:58"
Hope it helps!
I am using postrges db.
My domain has a date field:
java.util.Date requestedDate;
I am trying to search by date in my controller:
eq ("requestedDate", requestedDate)
This works fine, but the problem is that date and time has to be exactly matching for this. But in application the user will only enter the date to search items (like give me all requests which are made on 2014-02-05 and the browser application will add the current time to the request). So the comparison fails because the user entered time is different from the time during creation of the request.
I tried 'like' but it throws error.
How to compare only date part ?
You could do something like this:
Date now = new Date()
now.clearTime()
def results = Meeting.withCriteria {
between('date', now, now+1)
}
So this strips off the time portion of the current date, and then does a 'between' query (between midnight just gone and midnight 24 hours later).
Still it looks like there is no convenient way to realize this.
You need a small detour by computing the start of the day and the end of the day and use the between operator.
EDIT
I just saw now rcgeorge23 gave you the right example for doing this.
My Code works. The problem I have here is that dates like November 31 and April 31 can occur every year from 2010 down to 1995 which means I have to type in all these dates e.g [Date.new(2012,09,31),Date.new(2011,09,31)] and so on. I am trying to make this validation work for only the month and the day to avoid too much typing or in other words shorten my code please see a description of my
Model/profile.rb
validate :excluded_dates
private
def excluded_dates
exclusion_dates = [Date.new(2012,9,31), Date.new(2012,2,1)]
if exclusion_dates.include?(self.next_shoeing)
self.errors.add(:next_shoeing, "cannot be on a reserved date.")
end
First of all doing Date.new(2012,9,31) will throw you an ArgumentError: invalid date. IMO there is no need to validate this as no one will be able to construct such date (just handling this exception outside this model).
So if next_shoeing is an instance of Date it won't be invalid anyway.
I'm writing an app that keeps track of school classes.
I need to store the schedule. For example: Monday-Friday from 8:am-11am.
I was thinking about using a simple string column but I'm going to need to make time calculations later.
For example, I need to store a representation of 8am, such as start_at:8am end_at:11am
So how should I store the time? What datatype should I use? Should I store start time and number of seconds or minutes and then calculate from there? or is there an easier way?
I use MySQL for production and SQLite for development.
I made an app recently that had to tackle this problem. I decided to store open_at and closed_at in seconds from midnight in a simple business hour model. ActiveSupport includes this handy helper for finding out the time in seconds since midnight:
Time.now.seconds_since_midnight
This way I can do a simple query to find out if a venue is open:
BusinessHour.where("open_at > ? and close_at < ?", Time.now.seconds_since_midnight, Time.now.seconds_since_midnight)
Any tips for making this better would be appreciated =)
If you're using Postgresql you can use a time column type which is just the time of day and no date. You can then query
Event.where("start_time > '10:00:00' and end_time < '12:00:00'")
Maybe MySQL has something similar
Check out the gem 'tod' for Rails 4 or Time_of_Day for Rails 3. They both solve the problem of storing time in a database while using an an Active Record model.
SQL has a time data type but Ruby does not. Active Record addresses this difference by representing time attributes using Ruby’s Time class on the canonical date 2000-01-01. All Time attributes are arbitrarily assigned the same dates. While the attributes can be compared with one another without an issue, (the dates are the same), errors arise when you attempt to compare them with other Time instances. Simply using Time.parse on a string like ”10:05” adds today’s date to the output.
Lailson Bandeira created a created solution for this problem, the Time_of_Day gem for Rails 3. Unfortunately the gem is no longer maintained. Use Jack Christensen’s ‘tod’ gem instead. It works like a charm.
This ruby gem converts time of day to seconds since midnight and back. The seconds value is stored in the database and can be used for calculations and validations.
Define the time of day attributes:
class BusinessHour < ActiveRecord::Base
time_of_day_attr :opening, :closing
end
Converts time of day to seconds since midnight when a string was set:
business_hour = BusinessHour.new(opening: '9:00', closing: '17:00')
business_hour.opening
=> 32400
business_hour.closing
=> 61200
To convert back to time of day:
TimeOfDayAttr.l(business_hour.opening)
=> '9:00'
TimeOfDayAttr.l(business_hour.closing)
=> '17:00'
You could also omit minutes at full hour:
TimeOfDayAttr.l(business_hour.opening, omit_minutes_at_full_hour: true)
=> '9'
I would store the starting hour and the duration within the database, using two integer columns.
By retrieving both values, you could convert the starting hour as in (assuming that you know the day already:
# assuming date is the date of the day, datetime will hold the start time
datetime = date.change({:hour => your_stored_hour_value , :min => 0 , :sec => 0 })
# calculating the end time
end_time = datetime + your_stored_duration.seconds
Otherwise, hava a look at Chronic. The gem makes handling time a little bit easier. Note that the changemethod is part of rails, and not available in plain Ruby.
The documentation on DateTime for plain Ruby can be found here.
Also, whatever you do, don't start storing your dates/time in 12-hour format, you can use I18nin Rails to convert the time:
I18n.l Time.now, :format => "%I.%m %p", :locale => :"en"
I18n.l Time.now + 12.hours, :format => "%I.%m %p", :locale => :"en"
You can also get from this notation, that you can store you duration in hours, if you want, you can then convert them rather easily by:
your_stored_value.hours
if stored as an integer, that is.
Suggestion:
Don’t worry about a specific datatype for that. A simple solution would be:
In the database, add an integer type column for start_time and another for end_time. Each will store the number of minutes since midnight.
Ex: 8:30am would be stored as 510 (8*60+30)
In the form, create a select field (dropdown) that displays all available times in time format:Ex.: 10am, 10:30am and so on.
But the actual field values that get saved in the database are their integer equivalents:
Ex: 600, 630 and so on (following the example above)
I assume you are using some kind of database for this. If you are using MySQL or Postgresql, you can use the datetime column type, which Ruby/Rails will automatically convert to/from a Time object when reading/writing to the database. I'm not sure if sqlite has something similar, but I imagine it probably does.
From the SQLite 3 website,
"SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions."
You can then manipulate the values using the Date and Time functions outlined here.
For some reason:
Analytic.where({:ga_date.gte => '2010-09-01'}).count() # greater than or equal to
gives back 0, but
Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count()
gives back 230, which is the number of records (documents).
Actually, the first line on the top works in another case, so it is quite strange.
Can only the date be compared, because if it is
Analytic.where({:ga_date.lte => Time.parse('2010-09-10')}).count() # less than or equal to
then all the records with date 2010-09-10 will not be counted because Time.parse('2010-09-10') will give 2010-09-10 00:00:00, so the records will all have to be 2010-09-09 before the midnight. In other words, 2010-09-10 2am won't be included because 2am is not "less than or equal to" 00:00:00. It can be hacked by using
Analytic.where({:ga_date.lte => Time.parse('2010-09-10 23:59:59')}).count()
but it is kind of ugly. If there is a way to compare by date only like the first line of code in this post?
I think that you have two separate issues here.
Different data types
The following two lines are not equivalent. The first is a string comparison. The second is a comparison with a date object.
Analytic.where({:ga_date.gte => '2010-09-01'}).count()
Analytic.where({:ga_date.gte => Time.parse('2010-09-01')}).count()
I think you have figured this out, but it's important to be clear here. If you are storing date objects in the DB, you need to perform comparisons with date objects.
MongoDB will compare types and data.
Mismatch date storage
You are storing dates that have information for hours, minutes and seconds. However, you don't like the following notation:
:ga_date.lte => Time.parse('2010-09-10 23:59:59')
The workaround here is to use $lt and the day after.
:ga_date.lt => (Time.parse('2010-09-10') + 1.day) # or (60 * 60 * 24)
to add,
it is not strangely works, its coincidentally works when it just happens the string representation of the date happens to also lexicographically be 'greater than' the other date
other issue,
try to use only as much data fields as needed
if you meant it to be "within the calendar day",
what I usually like is to call beginning_of_day in both cases to equalize
this has the effect of neutralizing the minutes
else if you really meant within a 24h strike zone,
use ActiveSupport's '+ 1.day'