Skip to content

Commit

Permalink
Merge pull request #8 from josefzacek/generate-ChatroomUser-and-Messa…
Browse files Browse the repository at this point in the history
…ge-model-and-setup-associations

Generate ChatroomUser and Message model and setup associations
  • Loading branch information
josefzacek authored Aug 1, 2016
2 parents 1e65547 + 3e74c60 commit 7cf9ea9
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/models/chatroom.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class Chatroom < ApplicationRecord
has_many :chatroom_users
has_many :users, through: :chatroom_users
has_many :messages
end
4 changes: 4 additions & 0 deletions app/models/chatroom_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ChatroomUser < ApplicationRecord
belongs_to :chatroom
belongs_to :user
end
4 changes: 4 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Message < ApplicationRecord
belongs_to :chatroom
belongs_to :user
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable

has_many :chatroom_users
has_many :chatrooms, through: :chatroom_users
has_many :messages
end
10 changes: 10 additions & 0 deletions db/migrate/20160801191155_create_chatroom_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateChatroomUsers < ActiveRecord::Migration[5.0]
def change
create_table :chatroom_users do |t|
t.references :chatroom, foreign_key: true
t.references :user, foreign_key: true

t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20160801191235_create_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateMessages < ActiveRecord::Migration[5.0]
def change
create_table :messages do |t|
t.references :chatroom, foreign_key: true
t.references :user, foreign_key: true
t.text :body

t.timestamps
end
end
end
25 changes: 24 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,33 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160801184103) do
ActiveRecord::Schema.define(version: 20160801191235) do

create_table "chatroom_users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "chatroom_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["chatroom_id"], name: "index_chatroom_users_on_chatroom_id", using: :btree
t.index ["user_id"], name: "index_chatroom_users_on_user_id", using: :btree
end

create_table "chatrooms", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "messages", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.integer "chatroom_id"
t.integer "user_id"
t.text "body", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["chatroom_id"], name: "index_messages_on_chatroom_id", using: :btree
t.index ["user_id"], name: "index_messages_on_user_id", using: :btree
end

create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand All @@ -41,4 +60,8 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end

add_foreign_key "chatroom_users", "chatrooms"
add_foreign_key "chatroom_users", "users"
add_foreign_key "messages", "chatrooms"
add_foreign_key "messages", "users"
end

0 comments on commit 7cf9ea9

Please sign in to comment.