Skip to content

Commit

Permalink
Use has_many through instead of has_and_belongs_to_many
Browse files Browse the repository at this point in the history
  • Loading branch information
mirrec committed Oct 17, 2023
1 parent dcea3b7 commit bea0a0f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
# updated_at :datetime not null

class Message < ApplicationRecord
has_and_belongs_to_many :tags
has_many :messages_tags, dependent: :destroy
belongs_to :thread, class_name: 'MessageThread', foreign_key: :message_thread_id
belongs_to :author, class_name: 'User', foreign_key: :author_id, optional: true
has_many :messages_tags, dependent: :destroy
has_many :tags, through: :messages_tags
has_many :objects, class_name: 'MessageObject', dependent: :destroy
has_many :attachments, -> { where(object_type: "ATTACHMENT") }, class_name: 'MessageObject'
# used for joins only
Expand Down
8 changes: 5 additions & 3 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

class Tag < ApplicationRecord
belongs_to :tenant
has_and_belongs_to_many :messages
has_and_belongs_to_many :message_threads
belongs_to :owner, class_name: 'User', optional: true, foreign_key: :user_id
has_many :tag_users, dependent: :destroy
has_many :users, through: :tag_users
has_many :tag_groups, dependent: :destroy
has_many :groups, through: :tag_groups
belongs_to :owner, class_name: 'User', optional: true, foreign_key: :user_id
has_many :messages_tags
has_many :messages, through: :messages_tags
has_many :message_threads_tags
has_many :message_threads, through: :message_threads_tags

validates :name, presence: true
validates :name, uniqueness: { scope: :tenant_id, case_sensitive: false }
Expand Down

0 comments on commit bea0a0f

Please sign in to comment.