I have a query regarding sorting by date, I can easily sort the data I have based on a field by adding
.order('search_date desc')
Search date is the date in full - dd/mm/yyyy, but I do not want to add the year to this, I only want to order by DD/MM.
Does anyone know the best way to do this?
It's more a sql solution than a rails solution :
If you are using mysql :
.order("DATE_FORMAT(search_date, '%d%m')")
will sort by day and month
Related
I use php and dynatable.js (version 0.3.1) to create dynamic tables with a sort and search function. This setup works very well even with the U.S. formatted date from the SQL database. But I need the german date format in the table (dd.mm.yy or dd.mm.yyyy) to sort the entries.
I can't find a solution for my problem in the documentation from dynatable.js. Has someone had a similar problem with the date format in dynatable and maybe a hack or other kind of solution?
I'm kinda stuck in a select query question:
I have a Bill model, which contains two integer attributes: month and year. I would like to retrieve the more recent record (highest date) so I can check an attribute value on it. Any ideas for solving that problem, since month and year are independent attributes?
Thanks!
Bill.order('year DESC, month DESC').first
My initial thought was:
user.humans.where("created_at = ?", 10.days.ago)
Though, this seems to be looking for the record created 10 days ago at the exact time when the statement is called. I want to collect the records created on that day, regardless of their time.
Is anyone aware of a convenient way to do this? Let me know if I need to elaborate.
Thanks.
You'll probably want to use a range here, as I assume this is a datetime column.
User.humans.where("? <= created_at AND created_at <= ?", 10.days.ago.beginning_of_day, 10.days.ago.end_of_day)
You'll also want to make sure you're setting the time zone of your Rails application so that you're explicit about which time period you consider to be the 10th day.
Whichever DBMS you are using will have a method to convert a datetime to a date. You should then compare this to a date in ruby. For example, if your DBMS is MySQL you could say
user.humans.where("date(created_at) = ?", 10.days.ago.to_date)
If you're not using MySQL then you should be able to google converting a datetime to a date in your DBMS of choice.
I want to allow teachers to be able to login to my Rails 3.2 app and be able to set when they are available. So instead of having two datetime fields where an actual date is stored is stored for starts_at and ends_at, I'd like for them just to say I'm available on "Mondays between 4:00pm and 5:00pm" with all three values being dropdowns.
The orignal way I approached this was having a string for day and using the time_select method in my form for my starts_at and ends_at. Unfortunately, time_select still comes with the date.
I'm just looking for the cleanest way to allow weekly scheduling. Is this possible? If it is, is there an easier way to do this? Thanks in advance for your tips.
take a look at
https://github.com/mzararagoza/rails-fullcalendar-icecube
its a small appointment app that i think that you can take allot out of it.
I'm scoping out how to handle user-generated dates with rails and was hoping to get some thoughts on the best way to handle them. Ultimately I want to display dates/times provided by the user in HTML, and I also want to sort by the dates (ascending and descending, depending). My initial thought was to create the db as time: string and date:string and then convert these strings to datetime values. Is there a better way to go about this? I'm using RoR 3.1.
Any thoughts and ideas would be greatly appreciated so I don't start down the wrong path only to realize at a later date.
Yes, there is a better way to go about it. ActiveRecord supports the following types for times:
datetime
timestamp
time
date
So pick the type that matches your intent (time for time of day, date for a date, ...) and then let AR do its job and deal with the details. Then you'll get the right data in the database and the right objects in your Ruby; once you have the right objects in your Ruby everything will sort and compare and what not properly.