Rails 4.1
I'm am trying to add a date attribute to an ActiveRecord object by handing it a string and I'm getting some strange results:
t = MyClass.new
t.StartDate = "1/11/2015" #date is loaded as expected
t.StartDate = "1/12/2015" #date is loaded as expected
t.StartDate = "1/13/2015" #ArgumentError: argument out of range
The same appears to hold true for any day of the month > 12. What am I missing here? Yes, I could parse the string into a Date object (and I've been able to do this successfully with the same problem dates as strings), but why does my method work for some valid dates and not others?
Your dates are formatted with day/month/year when your trying to use it like month/day/year
This is why you can't go further than 12 because 12 is representing the month
Related
I have a model with :birth_date of type date.
I've tried to put a string like 3 janvier 1968 (French language) into that field and somehow in database I saw that PostgreSQL or someone else converted it into a date!
I also tried some other dates like 3 février 1968 or like 3 fevrier 1968 which didn't work and turned out to be NULL in db.
I can't find information about this feature anywhere. How does this work?
Rails knows that attribute is a Date from the database definition, so it converts the string you give it to a Date. If you create a new instance of your model in the Rails console and assign to birth_date, you can show that it's already a Date even before you save it to the database:
m = Model.new # Use your model name
m.birth_date = "3 février 1968"
m.birth_date.class
The console should tell you that m.birth_date is a Date.
So the conversion to Date is done before you save the model to the database. Rails defines a String::to_date method that calls the Ruby ::Date.parse method, which converts various human-readable date strings into a Date (https://ruby-doc.org/stdlib-2.3.1/libdoc/date/rdoc/Date.html#method-c-parse). In the Rails source, you'll see that whatever you assign to a Date attribute is converted to a Date with the to_date method. So when you assign a String, it happens via String::to_date which calls Date.parse.
As you mentioned in your comment, Date.parse seems to take a fairly loose approach to the months when they're spelled out. I tried a variety of English, French, and Spanish months in Date.parse, and as long as the first three letters of the non-English month are the same as the English month, Date.parse will convert them. But if the first three letters are different, then Date.parse throws an error.
if you have a column in the database as type 'date', it will only save as a date. Rails does it's best to convert a string into a recognized date if possible. You should always pass the 'birth_date' data as a date (i.e. use a date_field). Otherwise, if you REALLY want to store it as a string, the birth_date column must be of type string in the database
Pretty new to Ruby, I am having trouble implementing some logic
I have a lot of dates (effective and expiration dates).
I also have 2 objects. An original object, and a new copied object (it is a copy of the original)
I want to loop through each pair of dates, and check the orignal objects dates fields to see if ti had any. If it did have dates, then I was to:
use the original objects expiration date as the new objects effective date
default the new objects expiration date to the new effective date + 1 year
Here is my code, I get this error:
undefined method `id' for nil:NilClass
CODE:
DATE_FIELDS = [:agency_effective_on, :agency_expire_on,
:field_effective_on, :field_expire_on,
:product_manager_effective_on, :product_manager_expire_on,
:officer_effective_on, :officer_expire_on,
:regional_officer_effective_on, :regional_officer_expire_on]
#copy_to = #copy_from.dup
DATE_FIELDS.each.slice(2) do |field|
if !#copy_from.send(field[0].to_sym).nil? #if the ORIGNAL date is not nil
#copy it to the new Objects dates
#copy_to.send("#{field[0]}=".to_sym, #copy_from.send(field[0]))
#copy_to.send("#{field[1]}=".to_sym, #copy_from.send(field[1]).to_date + 365)
end
end
Ok so I had each.slice instead of each_slice
I have a date of birth column on my user table that takes a DATE. As this datatype appears as YYYY-MM-DD, I assume that when inputting a date to the database it must have the format, for example: 2013-12-26.
I have seen methods on StackOverflow for creating a random DateTime in Ruby, such as here. However, after much searching I can't find a way to generate a random date without the time, for example in the past 100 years, and have it properly formatted for the DATE datatype. In Rails, what is the best way to generate a random date without the time?
This seems to work:
def rand_date(days)
date = Date.today-rand(days)
date.to_s(:db)
end
But is there a more elegant solution that comes with Rails? I am new to Rails and programming, so any assistance would be most helpful!
Your method is correct. If you are using Rails, there are some trivial improvements such as
def rand_date(days)
rand(days).days.ago(Date.today)
end
which is mostly equivalent to
def rand_date(days)
rand(days).days.ago.to_date
end
The second version is less efficient because it creates more Date/Time objects during the internal conversions.
Apply to_s(:db) if you need the Date to be formatted as String.
A different approach would require you to construct a date passing the result of a rand to Date.new.
This is in core ruby:
1 #!usr/bin/ruby
2
3 require 'date'.
4
5 10.times do |t|
6 random_year = Random.new.rand(2000..2014) # custom range for years
7 random_month =Random.new.rand(1..12)
8 random_day = Random.new.rand(1..30)
9 puts "#{Date.new(random_year,random_month,random_day)}"
10 end
2014-11-29
2010-10-20
2006-02-23
2009-09-17
2006-01-14
2009-01-06
2002-07-06
2005-11-05
2013-06-20
2005-12-02
Here is something I used to generate random birth dates when populating a customer database. It works on days, and in this example, gives random dates between 1967-01-09 and 1993-01-12 by using the Date#jd method:
Date.jd(2439500 + rand(9500))
You can twiddle the dates generated by setting the base (in this case 2439500, which is 1967-01-09) and the random number to increase or decrease the range of dates produced.
Example:
irb(main):043:0> 10.times { puts Date.jd(2439500 + rand(9500)) }
1973-06-07
1973-11-09
1983-07-27
1990-11-03
1967-06-18
1967-06-20
1970-07-28
1990-05-13
1986-11-26
1989-02-15
I'm using Rails and MongoMapper as my working platform.
I want to generate a custom key with the help of month and year. The possible format would be YYYYMM####,
YYYY is current YEAR which I can get as Date.today.strftime("%Y")
MM is current Month Which I can get as Date.tody.strftime("%m")
After that ### is incremented integer value
I get the last job with the code
jobForLastnum = Job.last(:order => :_id.desc)
lastJobNum = jobForLastnum.job_number
Now my question is I received the job_number as '201305100'
I want to split it with custom length like, ['2013','05','100']
I know how to split a string in ruby and I successfully did that but i got result as individual character like
['2','0','1','3','0','5','1','0','0']
With the help of this I could retrieve the year:
lastJobNum.to_s[0,4]
With the help of this I got the month:
lastJobNum.to_s[4,2]
But after that there is custom length string. How can I get all the data in a single array?
You can simply use ranges:
c = "2013121003"
[c[0..3], c[4..5], c[6..-1]]
You can also use String#unpack:
"20131210034".unpack("A4A2A*")
Or with regexp as suggested by tessi, using String#scan:
c = "2013121003"
c.scan(/(\d{4})(\d{2})(\d+)/)
In all cases, this will return an array with the year, month, and job id as strings.
A regexp can help you here.
jobNumber = 201305100
year, month, job_id = jobNumber.to_s.match(/(\d{4})(\d{2})(\d*)/)[1..3]
First, we convert the jobNumber to a String. Then we throw a regexp at it. The regexp has three capture groups ((\d{4}) four numbers for the year, (\d{2}) two numbers for the month, (\d*) any remaining number for the job_id).
The job_number.to_s.match(...) returns a MatchData object, which we can access by its first three capture groups with [1..3] (see the documentation).
Finally, we assign the resulting Array to our variables year, month, and job_id.
year
#=> 2013
month
#=> 05
job_id
#=> 100
I am using a js calendar that inserts a string into a text field for submission as a datetime value. Not sure if it matters but I am using SQLite for now.
I am setting the format as follows (in an initializer):
Time::DATE_FORMATS[:default] = '%m/%d/%Y %H:%M'
Time::DATE_FORMATS[:db] = '%m/%d/%Y %H:%M'
This isn't working because Rails is apparently validating the date using a different format.
For example, if I set it as follows:
myobject.my_datetime = '06/30/2012 00:00'
it ends up null (the SQL query itself generated by ActiveRecord sets it to NULL). If I set it to a day less than or equal to 12 the date part ends up correct (but the generated SQL query still shows the string with the day before the month, i.e. in the above example the SQL query has the string as '30/06/2012 05:00:000000').
As you can see it also seems to be adding 5 hours for UTC(?) even though I have config.time_zone set to eastern time.
Behavior is the same either from console or via form submission.
Any suggestions on what I am missing?
UPDATE:
When I follow jdoe's suggestion and do this:
myobject.my_datetime = Time.strptime('06/30/2012 08:00', '%m/%d/%Y %H:%M')
The generated query now looks correct (ignoring the 4 hour difference):
UPDATE "myjoin_table" SET "my_datetime" = '06/30/2012 04:00.000000' .....
and it winds up in the database that way.
But when I reload the object the value in the AR object is initialized to nil.
I guess I'll just parse the string piece by piece in my controller action and set the parameters to update_attributes manually. This is harder than it should be.
ActiveRecord stores date/time in UTC+00:00. If you're living in the +5 zone, ActiveRecord will subtract 5 hours on assigning automatically. So, your current time zone just says how many hours has to be added/subtracted from your local time.
About parsing. Did you try the following?
myobject.my_datetime = Time.strptime('06/30/2012 00:00', '%m/%d/%Y %H:%M')
Rails time/date settings are made for showing, not for assignment. It's not a brilliant idea to get strings from a user and assign them to your date/time attributes. You'd better use proper time objects in assignment instead of stings. The strptime method will raise the ArgumentError if some user is trying to be a hacker. Your record.date_time='string' fails silently.
UPD: Usage Example
p = Product.first
p.last_visited = Time.strptime('06/30/2012 00:00', '%m/%d/%Y %H:%M')
p.save # leads to UPDATE "products" SET "last_visited" = '2012-06-29 21:00:00.000000'
Product.last_visited.localtime.strftime('%m/%d/%Y %H:%M') # => "06/30/2012 00:00"
As you see, time is stored and retrieved w/o any problems.
UPD 2: I18n
Locate your config/locales/en.yml. Add the following to it:
en:
time:
formats:
default: '%m/%d/%Y %H:%M'
To parse your time use t(or translate) helper:
#time = Time.strptime('06/30/2012 00:00', t('time.formats.default'))
Use l(or localize) helper to show time in your format:
= l #time