Skip to content

Instantly share code, notes, and snippets.

@Laurentttrade
Forked from Joseph-N/_message.html.erb
Created February 2, 2016 15:59
Show Gist options
  • Select an option

  • Save Laurentttrade/cc616be44769473675ae to your computer and use it in GitHub Desktop.

Select an option

Save Laurentttrade/cc616be44769473675ae to your computer and use it in GitHub Desktop.
Tutorial code snippets for chat application in rails. Tutorial link http://goo.gl/l3e8zN
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :involving, -> (user) do
where("conversations.sender_id =? OR conversations.recipient_id =?",user.id,user.id)
end
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
class CreateConversations < ActiveRecord::Migration
def change
create_table :conversations do |t|
t.integer :sender_id
t.integer :recipient_id
t.timestamps
end
add_index :conversations, :sender_id
add_index :conversations, :recipient_id
end
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :conversations, :foreign_key => :sender_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment