I'm interested in display a record set of timeline type events in the format:
2011
APR
- Record Out
- Record Out
AUG
- Record Out
- Record Out
SEP
- Record Out
- Record Out
OCT
- Record Out
- Record Out
2009
OCT
- Record Out
- Record Out
The trick here being it only display the year if there are items in that year to show.. Same for a month, the month grouping only renders if there are records for that month to display. Does rails have any built in grouping for this type of output? Or would this be 100% custom to implement? Thanks for any pointers.
The grouping can be done by Ruby's built-in Enumerable#group_by method:
events.group_by(&:year).each do |year, year_events|
puts year, year_events
end
I assume you have event.year method. The same thing with grouping by months.
Related
I have an error message that needs to be translated along with parameters. What is the best way to do it in rails?
dates = '12th June - 14th June'
errors.add(:base, :reserved_dates)
From en.yml
activemodel:
errors:
models:
reserve_changes:
reserved_dates: "Dates changes from %{dates}"
Expected output:
Dates changes from 12th June - 14th June
When running this way I only get Dates changes from without the passed parameters.
You need to pass in the dates as an option to the errors.add:
dates = '12th June - 14th June'
errors.add(:base, :reserved_dates, dates: dates)
Otherwise the translator has no access to the string you want to substitute.
Let's say I have three users created on different dates. Now I want do have a graph of users progression. So I want to get something like:
{
Thu, 02 Nov 2017=>1,
Sat, 04 Feb 2017=>2,
Wed, 21 Mar 2018=>3
}
It's very similar to grouping by created_at::date, but I want to have number of all the records created before this date rather than number of items created exactly on this date.
How can I achieve this using group_by and aggregate functions in Postgresql? I need it for Ruby on Rails project, but I expect simple vanilla SQL, no maps and complex queries.
I am no rails expert and my solution requires loading a big relationship like:
#u = User.all
And then find the smallest creation date :
#start = #u.minimum("created_at")
Then you can calculate the number of days between creation and now:
#days = ((Time.now - #start)/1.day).to_i
Then you can calculate for each day the number of users already created:
#days.each do |day|
puts "day "+day.to_s
puts #u.where("created_at < ?", #start+day.day).count
end
Probably some easier solutions in SQL though. (Also you may take only a subset of users by choosing a range of dates not to exceed the server memory)
I need to figure out how to group results into different sections by date.
I have a page that has reservations with different dates, instead of having a table sorted by dates, I want to create each reservation in its own section on that date.
ex.
January 27
John Doe - 01/27/17
Jane doe - 01/27/17
January 28
Ricky Bobby - 01/28/17
January 29
Baby Jesus - 01/29/17
You can use group_by on a collection of dates, then pass in a method like .month or .yday
array = [Date.today, Date.today, Date.today.next, Date.new(2017,12,31)]
array.group_by(&:yday)
This returns the list of dates by the day in the year. Its basically equivalent to Hash[yday] = Array.new(matching_dates).
I have a table of Albums that has a date column named release_date.
I want to get a list of all the month + year combinations present along with the number of albums released in that month/year.
So, the output might be something like:
November 2016 - 11
October 2016 - 4
July 2016 - 19
December 2015 - 2
Ruby 2.3.1 w/ Rails 5 on Postgres 9.6, FWIW.
Database layer is where this task belongs, not Ruby:
Album.group("TO_CHAR(release_date, 'Month YYYY')").count
Why using database layer? Simply because it is lightning fast compared to nearly anything else, it is resource-efficient especially compared to Ruby, it scales perfectly and because having tons of Album records you can simply overload memory and never actually finish the processing.
I'm assuming your table is singular Album per Rails convention. If not, consider changing it.
Album.all.map { |album| [Date::MONTHNAMES[album.date.month], album.date.year].join(' ') }
.each_with_object(Hash.new(0)) { |month_year, counts| counts[month_year] += 1 }
Explanation:
The .map method iterates over the albums and returns an array of strings consisting of ["month year", "month year", ... ].
The .each_with_object method is a standard counting algorithm that returns a hash with a count for each unique array item.
I am trying to build a timetable setup to work in conjunction with one of my Models. Before I state my question, I will give some context into the problem.
I have a Model called Appointments with the following columns:
Name(String)
Start_time(time)
End_time(time)
Appt_Date(date)
Now I can have multiple appointments in 1 day.
For Example, let's say I have the following 2 Appointment objects:
Appointment 1:
Name: Makeup Appointment
Start_time: 11:00 am
End_time: 1:00 pm
Date: March 30, 2016
Appointment 2:
Name: Daily Meetup
Start-time: 2:00 pm
End_time: 3:00 pm
Date: March 30, 2016
I would like to implement a date-picker form where you can select a date and it would render 24 rows(1 for each hour of the day) and fill in the rows with the times not available based on the appointments on that day.
For example, if I select March 30, 2016 from the date-picker, I would like to render the 24 rows and have the rows for 11am-1pm and 2:00pm-3:00pm shaded out.
The setup is like google calendars(how time slots are colored in) but with a day-to-day basis. I don't need to be able to edit these rows. I just need to view them and have them rendered with colored cells based on Appointment objects for that specific day.
My issue is, I don't know where to begin to be able to design these 24 rows that interact with appointment objects. I was thinking that perhaps I build a helper method, however even that I am pretty lost. I would appreciate some guidance on how to approach this.
Below will help you:
1.Create a file in initializer which contains list of available time slots like 10 am to 10 pm.
2.For displaying date time field , use some jquery date time picker plugin.
3.Fire an ajax when clicked on datetime picker.
4.Ajax request will come on the controller & where you have to create an active record query which will fetch all apointments according to the particular date.
5.MAke an array variable & store the time which are already booked in the above appointments.
6.Compare the available time with the booked time & edit datetimepikcer.js to shaded the booked date.