Skip to content
Create a gist now

Instantly share code, notes, and snippets.

# 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"
# With the following as its model:
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :notificationable, polymorphic: true
# 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'
# 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"
# 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
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.