ruby on rails - PostgreSQL - must appear in the GROUP BY clause or be used in an aggregate function -
this query works locally, doesn't work on heroku reason.
2014-06-26t00:45:11.334388+00:00 app[web.1]: pg::groupingerror: error: column "conversations.updated_at" must appear in group clause or used in aggregate function
this sql.
conversation.joins("inner join notifications on notifications.conversation_id = conversations.id , notifications.type in ('message') inner join receipts on receipts.notification_id = notifications.id notifications.type = 'message' , (receipts.receiver_id = #{a.id} , receipts.receiver_type = 'profile') , conversations.id in #{active_conversations_ids} order conversations.updated_at desc")
i tried doing distinct , did not work, , tried group_by('conversation_id')
the to_sql
select "conversations".* "conversations" inner join notifications on notifications.conversation_id = conversations.id , notifications.type in ('message') inner join receipts on receipts.notification_id = notifications.id notifications.type = 'message' , (receipts.receiver_id = 104 , receipts.receiver_type = 'profile') , conversations.id in (…) order conversations.updated_at desc"
you can see sql in server window, if running app in local machine.
it practice keep joins belong to.
conversation.where('id' => active_conversations_ids).joins( "inner join notifications on notifications.conversation_id = conversations.id , notifications.type in ('message') inner join receipts on receipts.notification_id = notifications.id , (receipts.receiver_id = #{a.id} , receipts.receiver_type = 'profile')" ).order('updated_at')
and better readability, can use notifications symbols.
conversation .find_all_by_id(active_conversations_ids) .joins(:notifications, :receipts).where({ notifications: { type: 'message' }, receipts: { receiver_id: a.id, receiver_type: 'profile'} }) .order('updated_at')
my final suggestion google named scopes.
http://zachholman.com/2010/01/simplifying-rails-controllers-with-named_scopes/
Comments
Post a Comment