Ruby on Rails Saturday, February 21, 2015

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:

@pin_albums = Pin.group(:album).order('artist asc')

The error in my log is:

ActionView::Template::Error (PG::GroupingError: ERROR:  column "pins.id" must appear in the GROUP BY clause or be used in an aggregate function

2015-02-21T20:42:53.231456+00:00 app[web.1]: LINE 1: SELECT "pins".* FROM "pins"  GROUP BY album  ORDER BY artist...

2015-02-21T20:42:53.231458+00:00 app[web.1]:                ^

2015-02-21T20:42:53.231460+00:00 app[web.1]: : SELECT "pins".* FROM "pins"  GROUP BY album  ORDER BY artist asc):

2015-02-21T20:42:53.231462+00:00 app[web.1]:     12:         <th></th>

2015-02-21T20:42:53.231464+00:00 app[web.1]:     13:         <th>Album</th>

2015-02-21T20:42:53.231466+00:00 app[web.1]:     14:       </tr>

2015-02-21T20:42:53.231469+00:00 app[web.1]:     15:     <% @pin_albums.each do |pin| %>  

2015-02-21T20:42:53.231471+00:00 app[web.1]:     16:         <tr>

2015-02-21T20:42:53.231473+00:00 app[web.1]:     17:           <td><%= (last_artist == pin.artist)  ?  ''   :   pin.artist %></td>

2015-02-21T20:42:53.231475+00:00 app[web.1]:     18:           <td><%= link_to image_tag(pin.image), pin %></td>

2015-02-21T20:42:53.231478+00:00 app[web.1]:   app/views/pages/artists.html.erb:15:in `_app_views_pages_artists_html_erb__4200071474432892294_70083862737420'



I've been Googling it, but any answers I've found haven't been clear enough for me to fix the issue. I see it says that pins.id must appear in the Group By clause, but I'm not clear on that.


On Fri, Feb 20, 2015 at 1:22 PM, TTambe <tambe257@gmail.com> wrote:
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! 

On Fri, Feb 20, 2015 at 3:57 AM, Colin Law <clanlaw@gmail.com> wrote:
On 20 February 2015 at 00:01, TTambe <tambe257@gmail.com> wrote:
> I actually ended up doing this which worked:
>
>     <% last_artist ||= nil %>
>
>   <table id="artist">
>     <tr>
>       <th>Artist</th>
>       <th></th>
>       <th>Album</th>
>    </tr>
> <% @pin_albums.each do |pin| %>
>   <% if last_artist != pin.artist %>
>     <tr>
>       <td><%= pin.artist %></td>
>       <td><%= link_to image_tag(pin.image), pin %></td>
>       <td><%= link_to pin.album, copy_pin_path(pin) %></td>
>     </tr>
>   <% else %>
>     <tr>
>       <td></td>
>       <td><%= link_to image_tag(pin.image), pin %></td>
>       <td><%= link_to pin.album, copy_pin_path(pin) %></td>
>     </tr>
>   <% end %>
>   <% last_artist = pin.artist %>
> <% end %>

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.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuBuJUBego%3Dq_5jDGaBRCA0rVHRwHdZDwjKrg665Hbqiw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAEdg5qS0PbX1OQ89iVc4ZTa40HtWA-LZjkno9H96t9P-BN6Qng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment