Note that there are some explanatory texts on larger screens.

plurals
  1. POrails query join, order by, group by issue
    primarykey
    data
    text
    <p>Have three models in project</p> <ul> <li>conversation</li> <li>conversation_message</li> <li>message</li> </ul> <p><strong>Conversation.rb</strong></p> <pre><code>class Conversation &lt; ActiveRecord::Base #subject has_many :conversation_messages, :dependent =&gt; :destroy has_many :messages, :through =&gt; :conversation_messages, :dependent =&gt; :destroy, :order =&gt; "created_at DESC" end </code></pre> <p><strong>ConversationMessage.rb</strong></p> <pre><code>class ConversationMessage &lt; ActiveRecord::Base #conversation_id, message_id belongs_to :conversation belongs_to :message, :dependent =&gt; :destroy end </code></pre> <p>Message.rb</p> <pre><code>#sender_id, receiver_id, message has_one :conversation_message, :dependent =&gt; :destroy has_one :real_conversation, :through =&gt; :conversation_message, :source =&gt; "conversation" </code></pre> <p>So I want to retrieve <code>current_user</code>'s all conversations and want to show them in order of last message received or sent. Also want to show single conversation for all of its messages and conversation must ordered by messages included in it. Using rails 3.0</p> <p>I tried following query but gives me an error below query</p> <pre><code>@user_conversations = Conversation .joins(:messages) .where(["messages.receiver_id = ? or messages.sender_id = ?", current_user.id, current_user.id ]) .group("conversations.id").order("messages.created_at DESC") </code></pre> <p>error</p> <pre><code>PG::GroupingError: ERROR: column "messages.created_at" must appear in the GROUP BY clause or be used in an aggregate function </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload