In ruby on rails Is there a way to dictate the format of fields inserted into the database via Activerecord? - ruby

I'm inserting some strings (that look dates) into a sqlite3 database via my ActiveRecord and the database is altering the field and making it look like a date. For example:
"4-2" => "2-Apr" #Not only a date but the order has been reversed.
"6-7-12" => "7/12/2006" #Again not only a date but the order has been changed.
I looked for a ruby string function that would prevent this from happening but haven't been able to come up with anything.
Has anyone else come across something like this and are you aware of any workarounds or fixes.

If you can predict the format of the string, use the following code to parse the string into date object before saving it.
Date.strptime '6-7-12', '%m-%d-%y'
=> Thu, 07 Jun 2012

Related

Need help formatting date into my activerecord migration column

I am working on a CLI app based on ActiveRecord migration, and I am trying to set one of the table columns to have a t.date property so that the users can check open availability during certain dates. The trouble I am having is how to format my date when I create a new instance of my class. Created instance
As of now, I just have 2020 integer in there so I can test out my other methods but I will need to get this situated before I move on to the more advanced methods. I have tried to use mm/dd/yyyy format in "" and without, as well as mm-dd-yyyy and some other fomrats. Not sure it should be a string, or just integer values.
I hope I am asking my question correctly.
I'd suggest that you would be able to create the date in a variety of different ways before passing it to your function.
Please see the below image for some examples of ways you could create a date object.
Ruby date and datetime class - Tadayoshi Funaba 1998-2011

Sequel way of selecting all records of specific date

What's the best way to select all records of a specific date in Sequel ignoring the time?
Writing a where clause with range from the day's 0 to 23:59 seems to unlike it. Any other way to do?
Use:
Post.where(:created_at => (date.beginning_of_day..date.end_of_day))
where date is the Date object

Ruby gem Mysql2 return date type cannot be parsed

I got the result in hash format:
"date"=>#'<'Date: 4911851/2,0,2299161>
Anyone know how to parse this format?
I'd like to parse it in to a ruby Date object and do further processing.
The result is still an timestamp object as far as I know. Just use it as it is inside an Date or DateTime to extract any data.
Alle the best.

UTC DateTime problems

I currently store all dateTimes in the DB as UTC dates. Each users time zone offset is also stored in the DB. When I retrieve a Date it is converted back to their local date using this offset.
The problem occurs when I retrieve a date using an ajax call. The date (which is already converted using the offset) is, I think, returned as a Java Date object. The browser then decides to mess with my Date adding the clients computers time zone offset to the Date object. This is causing dates to be a day ahead of what they should be if the time component is more than 11.59am.
The only solution I can come up with is to pass them as strings in which case this of course wouldn't happen. This is a laaaast resort for me though and I would love to find a better solution or workaround for this problem.
Your browser is not messing with the dates given that browsers don't have a native date transfer variable. You have something else that is doing that. How are you sending your dates in ajax? Json? Json will only send numbers or strings. XML will only send strings.
Something is converting your sent date into a javascript date object, find out what it is.

What is the best way to format a date in JSON for Mongo DB storage

I have a date with a time. I'm using ruby, but the language shouldn't matter.
d = "2010-04-01 13:00:00"
What is the best way to format this date for Mongo DB? By 'best' I mean, is there a certain format I could use where Mongo would recognize it as a date and might give me more-advanced filtering optons?
ie: If formatted correctly, could I ask Mongo to return all records whose month is '04'?
Thanks!
You don't need to format dates at all; dates are a supported data type. Each client driver should support dates through their standard date type, including the ruby one.
For advanced queries like your example, you can use a javascript expression for the find specifier:
{"$where": "this.date.getMonth() == 3"}
In ruby you should use a Time instance, which will get stored as the BSON datetime type. You could use a $where clause like Coady mentions, but will be better to do a range query with $lt and $gt - less overhead and can leverage an index.
I don't like relying on mongo Date objects. I think Mongo is slower with 'date' objects than it is with other data types (such as integers).
I tend to use integers (if you need timezone, have a tz field too, then you have localized time):
document = {:some_timestamp => Time.now.to_i}
#collection.find({'some_timestamp' => {'$gte' => Time.now.to_i}})
Sometimes I just use the timestamp built into the BSON::ObjectId's:
id = BSON::ObjectId.from_time(Time.now)
#collection.find({'_id' => {'$lte' => id}})

Resources