Answer the question
In order to leave comments, you need to log in
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
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
def create
current_user.conversations.create!(user1_id: current_user.id, user2_id: @user.id)
end
Answer the question
In order to leave comments, you need to log in
Already there: josephndungu.com/tutorials/gmail-like-chat-applic...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question