Search code examples
ruby-on-railsdatabasemodelpg

How to fix PGError in rails for ActiveRecord?


I am trying to get the unique values in the column 'genre' in Rails. The commented out section works on localhost but in production I get a PG Error: ActiveRecord::StatementInvalid (PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list . I found this but am not quite sure on how to apply it to my case as I'm looking for unique values in a specific column. Any advice?

    @iosapps = Iosapp.where(:user_id => me.id, :payment => 'free', :updated_at => days_ago..present ).order('updated_at DESC')
    @categories = Iosapp.order('updated_at DESC')
.where(:user_id => me.id)
.where(:payment => 'free')
.where(:updated_at => days_ago..present)
.select("distinct genre")

Error in the view:

2013-05-16T03:32:05.772029+00:00 app[web.2]:     51:             Category:
2013-05-16T03:32:05.772029+00:00 app[web.2]:     52:             <select id = 'category'>
2013-05-16T03:32:05.772029+00:00 app[web.2]:     53:                 <option value = "All">All</option>
2013-05-16T03:32:05.772029+00:00 app[web.2]:     54:                 <% @categories.each do |genre| %>
2013-05-16T03:32:05.772029+00:00 app[web.2]:     55:                     <option value = "<%= genre.genre %>"><%= genre.genre %></option>
2013-05-16T03:32:05.772318+00:00 app[web.2]:     56:                 <% end %>
2013-05-16T03:32:05.772318+00:00 app[web.2]:     57:             </select>
2013-05-16T03:32:05.772318+00:00 app[web.2]:   app/views/iosapps/test.html.erb:54:in `_app_views_iosapps_test_html_erb__2525281577349110042_49052520'

Solution

  • Maybe try

    Iosapp.order('updated_at DESC')
      .where(:user_id => me.id)
      .where(:payment => 'free')
      .where(:updated_at => days_ago..present)
      .select("distinct genre")
    

    Also i reccomend developing against the same db you are using in production