V
V
vdv_lg2015-03-18 13:05:24
Ruby on Rails
vdv_lg, 2015-03-18 13:05:24

How to implement the dialog model in Rails?

I want to implement a dialog model in my application (exactly like VK). There are User, Conversation, Message models.

class User < ActiveRecord::Base
   has_and_belongs_to_many :conversations, dependent: :destroy
   has_many :messages, through: conversations

class Conversation < ActiveRecord::Base
   has_and_belongs_to_many :users
   has_many :messages

class Message < ActiveRecord::Base
   belongs_to :user
   belongs_to :conversation

We also need a join table for users and conversations:
def change
    create_table :conversations_users do |t|
      t.integer :user_id
      t.integer :conversations_id

      t.timestamps null: false
    end
  	add_index :conversations_users, :user_id
    add_index :conversations_users, :conversation_id
    add_index :conversations_users, [:user_id, :conversation_id]
  end
end

When I want to write to @user for the first time, a dialog is created:
def create
 current_user.conversations.create!(user1_id: current_user.id, user2_id: @user.id)
end

It is necessary that this @user has a dialogue (common with mine), in which we would exchange messages.
I understand that when users correspond, everyone has their own dialogue with each other, in which they can delete unnecessary messages.
I do not understand a little how to build this architecture. Ruby gentlemen, I ask for your help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Semyon Semyonov, 2015-03-26
@vdv_lg

Already there: josephndungu.com/tutorials/gmail-like-chat-applic...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question