Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

notification_private_message_system

View gist:8364291
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# I've created a polymorphic table: notifications.
create_table "notifications", force: true do |t|
t.boolean "notification"
t.integer "user_id"
t.string "notificationable_type"
t.integer "notificationable_id"
end
 
# With the following as its model:
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :notificationable, polymorphic: true
end
 
# For PrivateMessage model, I created this:
 
class PrivateMessage < ActiveRecord::Base
has_many :notifications, as: :notificationable
belongs_to :sender, foreign_key: 'sender_uid', class_name: 'User'
belongs_to :receiver, foreign_key: 'receiver_uid', class_name: 'User'
end
 
# For the table, private_messages, I concocted the following:
create_table "private_messages", force: true do |t|
t.integer "sender_uid"
t.integer "receiver_uid"
t.text "subject"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
 
# Because the User model is important here, I will also show you it:
 
class User < ActiveRecord::Base
belongs_to :organization
has_many :project_users
has_many :projects, through: :project_users
has_many :notifications, as: :notificationable
has_many :sent_messages, class_name: 'PrivateMessage', foreign_key: 'sender_uid' :as => :notificationable
has_many :received_messages, class_name: 'PrivateMessage', foreign_key: 'receiver_uid' :as => :notificationable
 
validates :first_name, presence: true, uniqueness: true
validates :last_name, presence: true, uniqueness: true
validates :password, presence: true, on: :create, length: {minimum: 3}
 
has_secure_password validations: false
end
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.