This question already has answers here:
How do I group by day instead of date?
(6 answers)
Closed 8 years ago.
Im creating a time line with some columns from a table.
At the moment is the time line just display each column ordered by the "created_at" field.
But i want slice it for each day like this:
TODAY
content -|
|
|- content
|
content -|
|
YESTERDAY
|
|- content
|
content -|
At the moment am i using this code
<ul>
<% #timeline.each do |timeline| %>
<li>
<%= timeline.title %>
</li>
<% end %>
</ul>
I want to do something like the "each_slice(5)" function.
Hope anybody can tell me how to do this.
Thanks in advance.
You can group your array with ruby group_by:
#timeline.group_by { |el| el.created_at.to_date.to_s(:db) }
Note: I assume that #timeline already sorted.
Probably, this method will be reusable, so better place it in models like:
def self.to_grouped_by_day
group_by { |el| el.created_at.to_date.to_s(:db) }
end
And than call it in the controller.
Related
I am attempting to iterate over a range of years for a method that passes on unique years to a where clause in ruby on rails. For some odd reason, it does not fire off when I attempt to pass those numbers down to the method chain. For starters, I am working on a reporting feature that tracks charts by years in between a range. For example: Search starts at 2020 and ends at 2024. All years from 2020 to 2024 will show a report for each year compiled together. Right now, I only get one year and I should be getting 2020, 2021 and 2024 on one response..How can I print out a range of years by integer and pass them through the each block to the corresponding method?
Example code below:
params {start_year: "2019", end_year: "2020"}
params[:start_year], params[:end_year]
<% [#start_year.to_i+1...#end_year.to_i].each do |facility_year| %>
<% #facility.chart_records.where('extract(year from record_date) =?', facility_year).limit(11).each do |chart_record| %>
<tr>
<td><%= chart_record.record_date.strftime("%Y") %></td>
<td>Month: Jan</td>
<td><%= ChartRecord.where(facility_id: #facility.id).where('extract(month from record_date) =?', 1).where('extract(year from record_date) =?', facility_year).sum(:fit_jeans_count)</td>
</tr>
<% end %>
I just want to get one unique year from the start_year+1 count up until the end_year count and then display one record for each year in the range..
For ranges use parentheses (#start_year.to_i+1...#end_year.to_i) not brackets.
With brackets, you create an array with only one item that contains a range. With parentheses, you'll get a range right away.
I'm stuck on a tiny problem regarding chartkick. I have a rail app where you can create different currencies. You can then create a expense with a title, a amount and choose the currency from a list and the user_id. The relations are made and working. I have in my user controller something like this :
#user_spendings = #current_user.spendings.all.order('date DESC').paginate(:page => params[:page], :per_page => 15)
#sums_by_currency = Currency.joins(:spendings).
select(:symb, 'SUM(spendings.amount) AS amount').
where(spendings: { id: #user_spendings.map(&:id) }).
group(:symb)
And in my show view (as I want the expense from each user to be shown there) something like this :
<% #sums_by_currency.each do |currency| %>
<%= '%.02f' % "#{currency.amount}" %> <%= "#{currency.symb}" %>
<% end %>
That shows me the sum for each spending depending on the currency.
I would like to use this total and use chartkick to display the spending, with the date when this spending has been created.
I've tried several things already
First I went with this just to see :
<% #sums_by_currency.each do |currency| %>
<%= bar_chart currency.amount %>
<% end %>
Well I have to charts appearing but nothing shows up. Maybe the loop isn't the solution. Then I thought about the .map but I don't really know how to put that in place to be honnest.
I tried this aswell :
<%= line_chart #current_user.spendings.group(:date).sum(:amount) %>
That shows me the total spendings from all the currencies. I have to find out how to split all the currencies in different charts and show only the total amount from each currency.
If anyone can give me a clue I would appreciate it.
Thanks alot.
Ok guys I got it !
Took me 2 days only...
For the one interested in the answer here is what I did. I actually didn't change anything in the controller and I let the #sums_by_currency like it is.
Instead I went for that :
<%= column_chart #current_user.spendings.all.joins(:currency).group('currencies.symb').group_by_month(:date, format: "%B %Y").sum(:amount) %>
Give me all the spendings from the current_user from where I joined the currency that I grouped by symb. Then I grouped everything by month and I get the sum from the amount.
Yeah, you need to pass a set of data to the chart, not lots of individual pieces of data for individual charts (ie what you've got with the loop). Using map to convert your currency-objects to their amounts makes sense eg
<%= bar_chart #sums_by_currency.map{|c| c.amount } %>
or if you need a name something like:
<%= bar_chart #sums_by_currency.map{|c| {name: c.unit, data: c.amount} } %>
Where unit is whatever currency unit eg AUD
In my app to learn RoR, I want to get the last x (say 5) records in a list.
Currently I get all using
<% #business_partner.received_documents.each do |document| %>
Looking at documentation and SC posts, I tried using this code below, yet get an empty list.
<% #business_partner.received_documents.last(5) do |document| %>
what should I use?
Order received_documents by the creation date and take 5 last:
#business_partner.received_documents.order(:created_at).limit(5)
To get 5 newest ones you'd do:
#business_partner.received_documents.order(created_at: :desc).limit(5)
EDIT
The problem with this
#business_partner.received_documents.last(5) do |document|
is that you actually do not iterate over the collection, thus no output is shown.
Use each:
#business_partner.received_documents.last(5).each do |document|
You Forget to User each In Your Code
<% #business_partner.received_documents.last(5).each do |document| %>
I am creating a scheduling app in Rails and I am getting stuck while trying to organize the rooms by date. When I say a room, I essentially just mean a block in the schedule. A typical event might have a lunch (one room), then a networking section (another room). Here is how I am getting the rooms:
#rooms = Room.where(event_id: #current_event.id).order(:start_time)
So that returns the rooms that belong to an event, in order of the start time. In my view I loop through and display the rooms for the event, but I want to group them in the view by the date in case there is an event that is on multiple days.
Also :start_time is a datetime type in Ruby. In human speak, what I would do is look at the date portion and if the room date is not the same as the current group, I would print the new date and continue to group the rooms that fall on that day. Here is a trivial example in case I am not being clear:
Event: Staff Retreat
July 14th, 2015
-----------------------
12:30 PM
Team building Lunch Begins
------------------------
6:30 PM
Team building Dinner Begins
------------------------
July 15th, 2015
------------------------
9:30 AM
Team building Breakfast Begins
So having the grouping of rooms in the #rooms variable that is a datetime, what would be the best way to display a table like the above? I would think that in my loop I should check if the date was the same as the previous room, and if not print the date
- #rooms.each do |room|
room.start_time.strftime("%B %d, %Y")
room.start_time.strftime("%I:%M%p")
room.name
I am having trouble with the logistics because with the Model-View-Controller concept, I feel that sorting in the view may have the view do something it shouldn't have to. I am also struggling with how to do that in the view. I would think setting a variable in the controller that would hold the temporary date as I loop through would work, but it seems like that would start to get pretty messy though. What is the best way to group the various dates from the #rooms variable?
Can you try this. this will return you a hash where date is the key and value will contain all the #rooms related to that time.
#rooms = Room.where(event_id: #current_event.id)
#rooms = Hash[#rooms.sort_by{|o| o.start_time.to_date}.group_by {|room| room.start_time.to_date}.map{|l,m| [l, m.sort_by{|k| k.start_time}]}]
now you can traverse the rooms like this in the views. im putting code in erb format.
<% #rooms.each do |k, v| %>
<%= k %>
<% v.each do |room| %>
<%= room.created_at.start_time('%I:%M %p')%>
<%= #room.name or title what so ever. %>
<% end %>
<% end %>
Hi I'm having trouble with the below loop in a .erb view
<% my_list.each do | list | %>
.. loop stuff.....
<% end %.
This works fine for looping through my list, but I only want to loop through the first 4 items in 'my_list' and not the whole list. I tried some things like:
<% my_list.each do | product | 3.times %>
but didn't seem to work as I think my ruby knowledge is limited to say the least!
Use Array#take like this:
<% my_list.take(4).each do | product | %>
Take first 4 element from my_list Array#first:
<% my_list.first(4).each do | product | %>
use Array#each_slice for slice your array
<% my_list.each_slice(4) do | products | %>
<% products.each do | product | %>
It is apparent that you want to iterate through your list in groups of four (you really should amend your question, because this is an important piece of information). It is also apparent you are using Rails. Fortunately Rails has in_groups_of built into ActiveSupport:
<% my_list.in_groups_of(4) do |products| %>
<% products.each do | product | %>
One advantage of this approach (over alternatives such as each_slice) is that in_groups_of will pad the end to make sure you always get four groups. It will pad with nil by default, but you can specify the padding yourself:
<% my_list.in_groups_of(4, " ") do |products| %>
If you pass false as the pad, it will not pad at all and behaves just like each_slice.
<% my_list[0..3].each do | list | %>