You have three models in the project.
- Conversation
- conversation_message
- message
Conversation.rb
class Conversation < ActiveRecord::Base
ConversationMessage.rb
class ConversationMessage < ActiveRecord::Base
Message.rb
#sender_id, receiver_id, message has_one :conversation_message, :dependent => :destroy has_one :real_conversation, :through => :conversation_message, :source => "conversation"
So, I want to get current_user all the conversations and want to show them in the order they received or sent the last message. You also want to show a single conversation for all your messages, and the conversation must be ordered by the messages included in it. Using rails 3.0
I tried the following query but gave me an error below the query
@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")
Mistake
PG::GroupingError: ERROR: column "messages.created_at" must appear in the GROUP BY clause or be used in an aggregate function
join ruby-on-rails group-by order
chaitanya
source share