added room id to connections table
This commit is contained in:
parent
40c2b34ca3
commit
97aee2c10f
|
@ -2,13 +2,21 @@ class Connection < ApplicationRecord
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :contact, class_name: 'User', foreign_key: 'contact_id'
|
belongs_to :contact, class_name: 'User', foreign_key: 'contact_id'
|
||||||
|
|
||||||
|
after_create :set_room_id
|
||||||
after_create :create_inverted_connection
|
after_create :create_inverted_connection
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def set_room_id
|
||||||
|
self.room_id = "#{self.user.email}+#{self.contact.email}"
|
||||||
|
self.save!
|
||||||
|
end
|
||||||
|
|
||||||
def create_inverted_connection
|
def create_inverted_connection
|
||||||
unless Connection.where('user_id = ? and contact_id = ?', self.contact.id, self.user.id).length > 0
|
unless Connection.where('user_id = ? and contact_id = ?', self.contact.id, self.user.id).length > 0
|
||||||
Connection.create!(user: self.contact, contact: self.user)
|
inverted = Connection.create!(user: self.contact, contact: self.user)
|
||||||
|
inverted.room_id = "#{self.user.email}+#{self.contact.email}"
|
||||||
|
inverted.save!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddColumnToConnections < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :connections, :room_id, :string
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2018_08_27_151325) do
|
ActiveRecord::Schema.define(version: 2018_08_29_102007) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -18,6 +18,7 @@ ActiveRecord::Schema.define(version: 2018_08_27_151325) do
|
||||||
create_table "connections", force: :cascade do |t|
|
create_table "connections", force: :cascade do |t|
|
||||||
t.bigint "user_id"
|
t.bigint "user_id"
|
||||||
t.bigint "contact_id"
|
t.bigint "contact_id"
|
||||||
|
t.string "room_id"
|
||||||
t.index ["contact_id"], name: "index_connections_on_contact_id"
|
t.index ["contact_id"], name: "index_connections_on_contact_id"
|
||||||
t.index ["user_id"], name: "index_connections_on_user_id"
|
t.index ["user_id"], name: "index_connections_on_user_id"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue