In my music review app the Pins model has attributes of Pin.artist and Pin.album. I’m trying to list each artist reviewed on the site and which albums of theirs have been reviewed. Below is what I have so far, but I want to do it without repeating the artist name.
Controller:
@pin_albums = Pin.group(:album).order('artist')
View:
<% @pin_albums.each do |pin| %>
<%= pin.artist %> |
<%= link_to pin.album, copy_pin_path(pin) %>
<br/>
<% end %>
This lists them like this:
The Beatles | Let It Be The Beatles | Abbey Road Bob Dylan | Blood On The Tracks Bob Dylan | Highway 61 Revisited
I want to list them like so:
The Beatles | Let It Be
> Abbey Road
Bob Dylan | Blood On The Tracks
> Highway 61 Revisited
I need to do something to the effect of:
<% @pin_albums.each do |pin| %>
<ul>
<li><%= pin.artist %></li>
<ul>
<% pin.artist.each do |pin_album| %>
<li><%= link_to pin_album.album, pin_album %></li>
<% end %>
<br/>
<% end %>
I know that the above nested tables won’t work, but that’s the gist of what I’m trying to figure out.
This gives me the same result as my original code:
The Beatles | Let It Be
The Beatles | Abbey Road
Bob Dylan | Blood On The Tracks
Bob Dylan | Highway 61 Revisited
That looks as if it should work. Have you posted a direct copy/paste
from your code?
It assigns nil to last_artist unless last_artist already has a value,
then compares that to pin.artist. If not the same (so a new artist)
it assigns pin.artist to last_artist and displays that, if the the
same then it displays an empty string.
I wonder whether the precedence is wrong. If the above is a copy/paste
from your code then try
((last_artist ||= nil) != pin.artist) ? (last_artist = pin.artist) : ''
Note the extra parentheses
If that still doesn't work add a column on the end of the line showing
last_artist and see if it is tracking correctly.
Yes, the above was a paste from my actual code. I added the parentheses, but there was no change. I added <%= last_artist %> and the output was just each artist again. So it seems that there is something wrong with teh portion that checks last_artist against pin.artist.
You are right in that the key was to initialise last_artist at an
appropriate scope.
There are an awful lot of repeated lines there, Suppose you wanted to
change the detail of the contents of the last cell in each row, you
would have to change it in two places. You should put the if
statement round just the first cell not round the whole row. so for
that cell use something like
<td>
<%= (last_artist == pin.artist) ? '' : pin.artist %>
</td>
Or use an if statement inside the <td> </td> if you prefer. That is
two single quotes in the line above of course.
You’re absolutely right Colin, thanks. I’m not that familiar with that simplified syntax of if/else statements, so I learned quite a bit from this exchange!
Ok, so I need some help again. Every thing works great locally, but when I push to Heroku the controller code doesn’t play nice with the Postgres database. Here is the controller again:
I found this in the Postgres documentation, but it reads like jibberish to me for the most part.
GROUP BY Clause
The optional GROUP BY clause has the general form
GROUP BY `expression` [, ...]
GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions. expression can be an input column name, or the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a GROUP BY name will be interpreted as an input-column name rather than an output column name.
Aggregate functions, if any are used, are computed across all rows making up each group, producing a separate value for each group (whereas without GROUP BY, an aggregate produces a single value computed across all the selected rows). When GROUP BY is present, it is not valid for the SELECT list expressions to refer to ungrouped columns except within aggregate functions, since there would be more than one possible value to return for an ungrouped column.