From 42d2e5b3ad9bdaa07f5bd9861023b1febac60bcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 24 Oct 2019 04:41:04 +0200 Subject: [PATCH] Apply Rails/InverseOf rubocop rule Not doing so has a few gotchas when working with relations, particularly with records which are not stored in the database. I'm excluding the related content file because it's got a very peculiar relationship with itself: the `has_one :opposite_related_content` has no inverse; the relation itself is its inverse. It's a false positive since the inverse condition is true: ``` content.opposite_related_content.opposite_related_content.object_id == content.object_id ``` --- .rubocop.yml | 3 --- .rubocop_basic.yml | 5 +++++ app/models/budget/investment.rb | 9 +++++--- app/models/budget/phase.rb | 4 ++-- app/models/comment.rb | 2 +- app/models/concerns/documentable.rb | 2 +- app/models/concerns/flaggable.rb | 2 +- app/models/concerns/followable.rb | 2 +- app/models/concerns/galleryable.rb | 2 +- app/models/concerns/imageable.rb | 2 +- app/models/concerns/linkable.rb | 2 +- app/models/concerns/milestoneable.rb | 4 ++-- app/models/concerns/relationable.rb | 5 ++++- app/models/concerns/reportable.rb | 2 +- app/models/concerns/stats_versionable.rb | 2 +- app/models/dashboard/executed_action.rb | 2 +- app/models/debate.rb | 4 ++-- app/models/direct_message.rb | 4 ++-- app/models/legislation/annotation.rb | 4 ++-- app/models/legislation/draft_version.rb | 7 +++++-- app/models/legislation/people_proposal.rb | 6 +++--- app/models/legislation/process.rb | 25 +++++++++++++++++------ app/models/legislation/proposal.rb | 4 ++-- app/models/legislation/question.rb | 4 ++-- app/models/newsletter.rb | 2 +- app/models/poll.rb | 2 +- app/models/poll/answer.rb | 2 +- app/models/poll/officer.rb | 2 +- app/models/poll/pair_answer.rb | 2 +- app/models/poll/partial_result.rb | 2 +- app/models/poll/question.rb | 9 +++++--- app/models/proposal.rb | 6 +++--- app/models/site_customization/page.rb | 5 ++++- app/models/topic.rb | 2 +- app/models/user.rb | 23 ++++++++++++++------- app/models/votation_set_answer.rb | 2 +- app/models/widget/card.rb | 5 ++++- 37 files changed, 107 insertions(+), 65 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index ae7f59cd3..e96c53e69 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -21,9 +21,6 @@ Rails/HasAndBelongsToMany: Rails/HasManyOrHasOneDependent: Enabled: true -Rails/InverseOf: - Enabled: true - Rails/SkipsModelValidations: Enabled: true diff --git a/.rubocop_basic.yml b/.rubocop_basic.yml index 5e4e33887..d261ee85a 100644 --- a/.rubocop_basic.yml +++ b/.rubocop_basic.yml @@ -206,6 +206,11 @@ Rails/FindEach: Rails/HttpPositionalArguments: Enabled: true +Rails/InverseOf: + Enabled: true + Exclude: + - "app/models/related_content.rb" + Rails/NotNullColumn: Enabled: true Exclude: diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index b17e5f937..68010ec3d 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -32,7 +32,7 @@ class Budget translates :description, touch: true include Globalizable - belongs_to :author, -> { with_hidden }, class_name: "User" + belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :budget_investments belongs_to :heading belongs_to :group belongs_to :budget @@ -44,8 +44,11 @@ class Budget has_many :valuator_group_assignments, dependent: :destroy has_many :valuator_groups, through: :valuator_group_assignments - has_many :comments, -> { where(valuation: false) }, as: :commentable - has_many :valuations, -> { where(valuation: true) }, as: :commentable, class_name: "Comment" + has_many :comments, -> { where(valuation: false) }, as: :commentable, inverse_of: :commentable + has_many :valuations, -> { where(valuation: true) }, + as: :commentable, + inverse_of: :commentable, + class_name: "Comment" has_many :tracker_assignments, dependent: :destroy has_many :trackers, through: :tracker_assignments diff --git a/app/models/budget/phase.rb b/app/models/budget/phase.rb index d9318f27d..6432a13b1 100644 --- a/app/models/budget/phase.rb +++ b/app/models/budget/phase.rb @@ -12,8 +12,8 @@ class Budget include Sanitizable belongs_to :budget - belongs_to :next_phase, class_name: self.name - has_one :prev_phase, class_name: self.name, foreign_key: :next_phase_id + belongs_to :next_phase, class_name: self.name, inverse_of: :prev_phase + has_one :prev_phase, class_name: self.name, foreign_key: :next_phase_id, inverse_of: :next_phase validates_translation :summary, length: { maximum: SUMMARY_MAX_LENGTH } validates_translation :description, length: { maximum: DESCRIPTION_MAX_LENGTH } diff --git a/app/models/comment.rb b/app/models/comment.rb index 4a8351577..112ebb9fd 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -30,7 +30,7 @@ class Comment < ApplicationRecord validate :comment_valuation, if: -> { valuation } belongs_to :commentable, -> { with_hidden }, polymorphic: true, counter_cache: true, touch: true - belongs_to :user, -> { with_hidden } + belongs_to :user, -> { with_hidden }, inverse_of: :comments before_save :calculate_confidence_score diff --git a/app/models/concerns/documentable.rb b/app/models/concerns/documentable.rb index 220912cce..dcff3461d 100644 --- a/app/models/concerns/documentable.rb +++ b/app/models/concerns/documentable.rb @@ -2,7 +2,7 @@ module Documentable extend ActiveSupport::Concern included do - has_many :documents, as: :documentable, dependent: :destroy + has_many :documents, as: :documentable, inverse_of: :documentable, dependent: :destroy accepts_nested_attributes_for :documents, allow_destroy: true end diff --git a/app/models/concerns/flaggable.rb b/app/models/concerns/flaggable.rb index 6e2394d5b..650f679a0 100644 --- a/app/models/concerns/flaggable.rb +++ b/app/models/concerns/flaggable.rb @@ -2,7 +2,7 @@ module Flaggable extend ActiveSupport::Concern included do - has_many :flags, as: :flaggable + has_many :flags, as: :flaggable, inverse_of: :flaggable scope :flagged, -> { where("flags_count > 0") } scope :pending_flag_review, -> { flagged.where(ignored_flag_at: nil, hidden_at: nil) } scope :with_ignored_flag, -> { flagged.where.not(ignored_flag_at: nil).where(hidden_at: nil) } diff --git a/app/models/concerns/followable.rb b/app/models/concerns/followable.rb index 9e9957949..8a08d6809 100644 --- a/app/models/concerns/followable.rb +++ b/app/models/concerns/followable.rb @@ -2,7 +2,7 @@ module Followable extend ActiveSupport::Concern included do - has_many :follows, as: :followable, dependent: :destroy + has_many :follows, as: :followable, inverse_of: :followable, dependent: :destroy has_many :followers, through: :follows, source: :user scope :followed_by_user, ->(user) { diff --git a/app/models/concerns/galleryable.rb b/app/models/concerns/galleryable.rb index 5ddf322f6..b50e65675 100644 --- a/app/models/concerns/galleryable.rb +++ b/app/models/concerns/galleryable.rb @@ -2,7 +2,7 @@ module Galleryable extend ActiveSupport::Concern included do - has_many :images, as: :imageable, dependent: :destroy + has_many :images, as: :imageable, inverse_of: :imageable, dependent: :destroy accepts_nested_attributes_for :images, allow_destroy: true, update_only: true def image_url(style) diff --git a/app/models/concerns/imageable.rb b/app/models/concerns/imageable.rb index 3e3b15685..fffc871de 100644 --- a/app/models/concerns/imageable.rb +++ b/app/models/concerns/imageable.rb @@ -2,7 +2,7 @@ module Imageable extend ActiveSupport::Concern included do - has_one :image, as: :imageable, dependent: :destroy + has_one :image, as: :imageable, inverse_of: :imageable, dependent: :destroy accepts_nested_attributes_for :image, allow_destroy: true, update_only: true def image_url(style) diff --git a/app/models/concerns/linkable.rb b/app/models/concerns/linkable.rb index 1a58efacb..2bfbaff7b 100644 --- a/app/models/concerns/linkable.rb +++ b/app/models/concerns/linkable.rb @@ -2,7 +2,7 @@ module Linkable extend ActiveSupport::Concern included do - has_many :links, as: :linkable, dependent: :destroy + has_many :links, as: :linkable, inverse_of: :linkable, dependent: :destroy accepts_nested_attributes_for :links, allow_destroy: true end end diff --git a/app/models/concerns/milestoneable.rb b/app/models/concerns/milestoneable.rb index 0ed1b47f5..28cf33574 100644 --- a/app/models/concerns/milestoneable.rb +++ b/app/models/concerns/milestoneable.rb @@ -2,11 +2,11 @@ module Milestoneable extend ActiveSupport::Concern included do - has_many :milestones, as: :milestoneable, dependent: :destroy + has_many :milestones, as: :milestoneable, inverse_of: :milestoneable, dependent: :destroy scope :with_milestones, -> { joins(:milestones).distinct } - has_many :progress_bars, as: :progressable + has_many :progress_bars, as: :progressable, inverse_of: :progressable acts_as_taggable_on :milestone_tags diff --git a/app/models/concerns/relationable.rb b/app/models/concerns/relationable.rb index 85f396ebb..538ee8738 100644 --- a/app/models/concerns/relationable.rb +++ b/app/models/concerns/relationable.rb @@ -2,7 +2,10 @@ module Relationable extend ActiveSupport::Concern included do - has_many :related_contents, as: :parent_relationable, dependent: :destroy + has_many :related_contents, + as: :parent_relationable, + inverse_of: :parent_relationable, + dependent: :destroy end def find_related_content(relationable) diff --git a/app/models/concerns/reportable.rb b/app/models/concerns/reportable.rb index 45cc7b328..f584faa86 100644 --- a/app/models/concerns/reportable.rb +++ b/app/models/concerns/reportable.rb @@ -2,7 +2,7 @@ module Reportable extend ActiveSupport::Concern included do - has_one :report, as: :process, dependent: :destroy + has_one :report, as: :process, inverse_of: :process, dependent: :destroy accepts_nested_attributes_for :report end diff --git a/app/models/concerns/stats_versionable.rb b/app/models/concerns/stats_versionable.rb index 5f1d94ffd..9e9138ecc 100644 --- a/app/models/concerns/stats_versionable.rb +++ b/app/models/concerns/stats_versionable.rb @@ -2,7 +2,7 @@ module StatsVersionable extend ActiveSupport::Concern included do - has_one :stats_version, as: :process + has_one :stats_version, as: :process, inverse_of: :process end def find_or_create_stats_version diff --git a/app/models/dashboard/executed_action.rb b/app/models/dashboard/executed_action.rb index 9c020fc7d..e2ee5c46a 100644 --- a/app/models/dashboard/executed_action.rb +++ b/app/models/dashboard/executed_action.rb @@ -2,7 +2,7 @@ class Dashboard::ExecutedAction < ApplicationRecord belongs_to :proposal belongs_to :action - has_many :administrator_tasks, as: :source, dependent: :destroy + has_many :administrator_tasks, as: :source, inverse_of: :source, dependent: :destroy validates :proposal, presence: true, uniqueness: { scope: :action } validates :action, presence: true diff --git a/app/models/debate.rb b/app/models/debate.rb index eb0b20fbd..7512a5342 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -24,9 +24,9 @@ class Debate < ApplicationRecord translates :description, touch: true include Globalizable - belongs_to :author, -> { with_hidden }, class_name: "User" + belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :debates belongs_to :geozone - has_many :comments, as: :commentable + has_many :comments, as: :commentable, inverse_of: :commentable extend DownloadSettings::DebateCsv delegate :name, :email, to: :author, prefix: true diff --git a/app/models/direct_message.rb b/app/models/direct_message.rb index c5742fe9e..16c676756 100644 --- a/app/models/direct_message.rb +++ b/app/models/direct_message.rb @@ -1,6 +1,6 @@ class DirectMessage < ApplicationRecord - belongs_to :sender, class_name: "User" - belongs_to :receiver, class_name: "User" + belongs_to :sender, class_name: "User", inverse_of: :direct_messages_sent + belongs_to :receiver, class_name: "User", inverse_of: :direct_messages_received validates :title, presence: true validates :body, presence: true diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index 3384f059e..ebe7e0c10 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -5,9 +5,9 @@ class Legislation::Annotation < ApplicationRecord serialize :ranges, Array - belongs_to :draft_version, foreign_key: "legislation_draft_version_id" + belongs_to :draft_version, foreign_key: "legislation_draft_version_id", inverse_of: :annotations belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_annotations - has_many :comments, as: :commentable, dependent: :destroy + has_many :comments, as: :commentable, inverse_of: :commentable, dependent: :destroy validates :text, presence: true validates :quote, presence: true diff --git a/app/models/legislation/draft_version.rb b/app/models/legislation/draft_version.rb index 985b98b2c..1e079bb8e 100644 --- a/app/models/legislation/draft_version.rb +++ b/app/models/legislation/draft_version.rb @@ -9,8 +9,11 @@ class Legislation::DraftVersion < ApplicationRecord translates :body, touch: true include Globalizable - belongs_to :process, foreign_key: "legislation_process_id" - has_many :annotations, foreign_key: "legislation_draft_version_id", dependent: :destroy + belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :draft_versions + has_many :annotations, + foreign_key: "legislation_draft_version_id", + inverse_of: :draft_version, + dependent: :destroy validates_translation :title, presence: true validates_translation :body, presence: true diff --git a/app/models/legislation/people_proposal.rb b/app/models/legislation/people_proposal.rb index c9b79989f..a0a9fe461 100644 --- a/app/models/legislation/people_proposal.rb +++ b/app/models/legislation/people_proposal.rb @@ -19,9 +19,9 @@ class Legislation::PeopleProposal < ApplicationRecord acts_as_votable acts_as_paranoid column: :hidden_at - belongs_to :process, foreign_key: "legislation_process_id" - belongs_to :author, -> { with_hidden }, class_name: "User" - has_many :comments, as: :commentable + belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :people_proposals + belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :people_proposals + has_many :comments, as: :commentable, inverse_of: :commentable validates :title, presence: true validates :summary, presence: true diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index e7cdc7bbc..359c31001 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -25,13 +25,26 @@ class Legislation::Process < ApplicationRecord CSS_HEX_COLOR = /\A#?(?:[A-F0-9]{3}){1,2}\z/i - has_many :draft_versions, -> { order(:id) }, foreign_key: "legislation_process_id", dependent: :destroy + has_many :draft_versions, -> { order(:id) }, + foreign_key: "legislation_process_id", + inverse_of: :process, + dependent: :destroy has_one :final_draft_version, -> { where final_version: true, status: "published" }, - class_name: "Legislation::DraftVersion", - foreign_key: "legislation_process_id" - has_many :questions, -> { order(:id) }, foreign_key: "legislation_process_id", dependent: :destroy - has_many :proposals, -> { order(:id) }, foreign_key: "legislation_process_id", dependent: :destroy - has_many :people_proposals, -> { order(:id) }, foreign_key: "legislation_process_id", dependent: :destroy + class_name: "Legislation::DraftVersion", + foreign_key: "legislation_process_id", + inverse_of: :process + has_many :questions, -> { order(:id) }, + foreign_key: "legislation_process_id", + inverse_of: :process, + dependent: :destroy + has_many :proposals, -> { order(:id) }, + foreign_key: "legislation_process_id", + inverse_of: :process, + dependent: :destroy + has_many :people_proposals, -> { order(:id) }, + foreign_key: "legislation_process_id", + inverse_of: :process, + dependent: :destroy validates_translation :title, presence: true validates :start_date, presence: true diff --git a/app/models/legislation/proposal.rb b/app/models/legislation/proposal.rb index 133219591..f337bdb4e 100644 --- a/app/models/legislation/proposal.rb +++ b/app/models/legislation/proposal.rb @@ -19,10 +19,10 @@ class Legislation::Proposal < ApplicationRecord acts_as_votable acts_as_paranoid column: :hidden_at - belongs_to :process, foreign_key: "legislation_process_id" + belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :proposals belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_proposals belongs_to :geozone - has_many :comments, as: :commentable + has_many :comments, as: :commentable, inverse_of: :commentable validates :title, presence: true validates :summary, presence: true diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 17be22596..f54fa6648 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -7,12 +7,12 @@ class Legislation::Question < ApplicationRecord include Globalizable belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_questions - belongs_to :process, foreign_key: "legislation_process_id" + belongs_to :process, foreign_key: "legislation_process_id", inverse_of: :questions has_many :question_options, -> { order(:id) }, class_name: "Legislation::QuestionOption", foreign_key: "legislation_question_id", dependent: :destroy, inverse_of: :question has_many :answers, class_name: "Legislation::Answer", foreign_key: "legislation_question_id", dependent: :destroy, inverse_of: :question - has_many :comments, as: :commentable, dependent: :destroy + has_many :comments, as: :commentable, inverse_of: :commentable, dependent: :destroy accepts_nested_attributes_for :question_options, reject_if: proc { |attributes| attributes.all? { |k, v| v.blank? } }, allow_destroy: true diff --git a/app/models/newsletter.rb b/app/models/newsletter.rb index 021c8c7a8..be36dd2af 100644 --- a/app/models/newsletter.rb +++ b/app/models/newsletter.rb @@ -1,5 +1,5 @@ class Newsletter < ApplicationRecord - has_many :activities, as: :actionable + has_many :activities, as: :actionable, inverse_of: :actionable validates :subject, presence: true validates :segment_recipient, presence: true diff --git a/app/models/poll.rb b/app/models/poll.rb index 46655d1b8..b28c17b97 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -24,7 +24,7 @@ class Poll < ApplicationRecord has_many :officer_assignments, through: :booth_assignments has_many :officers, through: :officer_assignments has_many :questions, inverse_of: :poll, dependent: :destroy - has_many :comments, as: :commentable + has_many :comments, as: :commentable, inverse_of: :commentable has_many :ballot_sheets has_and_belongs_to_many :geozones diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 7f4ce8a8f..4beb3b73a 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -1,5 +1,5 @@ class Poll::Answer < ApplicationRecord - belongs_to :question, -> { with_hidden } + belongs_to :question, -> { with_hidden }, inverse_of: :answers belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_answers delegate :poll, :poll_id, to: :question diff --git a/app/models/poll/officer.rb b/app/models/poll/officer.rb index 1070e99af..0b8d56dc1 100644 --- a/app/models/poll/officer.rb +++ b/app/models/poll/officer.rb @@ -3,7 +3,7 @@ class Poll belongs_to :user has_many :officer_assignments has_many :shifts - has_many :failed_census_calls, foreign_key: :poll_officer_id + has_many :failed_census_calls, foreign_key: :poll_officer_id, inverse_of: :poll_officer validates :user_id, presence: true, uniqueness: true diff --git a/app/models/poll/pair_answer.rb b/app/models/poll/pair_answer.rb index 18728388c..59c7876dc 100644 --- a/app/models/poll/pair_answer.rb +++ b/app/models/poll/pair_answer.rb @@ -1,5 +1,5 @@ class Poll::PairAnswer < ApplicationRecord - belongs_to :question, -> { with_hidden } + belongs_to :question, -> { with_hidden }, inverse_of: :pair_answers belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_pair_answers belongs_to :answer_right, class_name: "Poll::Question::Answer" belongs_to :answer_left, class_name: "Poll::Question::Answer" diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index 1ec31d9e0..d608044cc 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -1,7 +1,7 @@ class Poll::PartialResult < ApplicationRecord VALID_ORIGINS = %w[web booth] - belongs_to :question, -> { with_hidden } + belongs_to :question, -> { with_hidden }, inverse_of: :partial_results belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_partial_results belongs_to :booth_assignment belongs_to :officer_assignment diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 62f01d429..f7314ac22 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -11,12 +11,15 @@ class Poll::Question < ApplicationRecord belongs_to :poll belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_questions - has_many :comments, as: :commentable + has_many :comments, as: :commentable, inverse_of: :commentable has_many :answers, class_name: "Poll::Answer" - has_many :question_answers, -> { order "given_order asc" }, class_name: "Poll::Question::Answer", dependent: :destroy + has_many :question_answers, -> { order "given_order asc" }, + class_name: "Poll::Question::Answer", + inverse_of: :question, + dependent: :destroy has_many :partial_results has_many :pair_answers - has_one :votation_type, as: :questionable + has_one :votation_type, as: :questionable, inverse_of: :questionable belongs_to :proposal attr_accessor :enum_type, :max_votes, :prioritization_type diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 90417d85c..c02e09bd1 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -35,13 +35,13 @@ class Proposal < ApplicationRecord include Globalizable translation_class_delegate :retired_at - belongs_to :author, -> { with_hidden }, class_name: "User" + belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :proposals belongs_to :geozone - has_many :comments, as: :commentable, dependent: :destroy + has_many :comments, as: :commentable, inverse_of: :commentable, dependent: :destroy has_many :proposal_notifications, dependent: :destroy has_many :dashboard_executed_actions, dependent: :destroy, class_name: "Dashboard::ExecutedAction" has_many :dashboard_actions, through: :dashboard_executed_actions, class_name: "Dashboard::Action" - has_many :polls, as: :related + has_many :polls, as: :related, inverse_of: :related extend DownloadSettings::ProposalCsv delegate :name, :email, to: :author, prefix: true diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index bb923b107..be59d0dcd 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -1,6 +1,9 @@ class SiteCustomization::Page < ApplicationRecord VALID_STATUSES = %w[draft published] - has_many :cards, class_name: "Widget::Card", foreign_key: "site_customization_page_id" + has_many :cards, + class_name: "Widget::Card", + foreign_key: "site_customization_page_id", + inverse_of: :page translates :title, touch: true translates :subtitle, touch: true diff --git a/app/models/topic.rb b/app/models/topic.rb index 1756969ef..426aa4653 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -6,7 +6,7 @@ class Topic < ApplicationRecord belongs_to :community belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :topics - has_many :comments, as: :commentable + has_many :comments, as: :commentable, inverse_of: :commentable validates :title, presence: true validates :description, presence: true diff --git a/app/models/user.rb b/app/models/user.rb index e5b2847f4..73388580b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,20 +21,29 @@ class User < ApplicationRecord has_one :lock has_many :flags has_many :identities, dependent: :destroy - has_many :debates, -> { with_hidden }, foreign_key: :author_id - has_many :proposals, -> { with_hidden }, foreign_key: :author_id - has_many :people_proposals, -> { with_hidden }, foreign_key: :author_id + has_many :debates, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author + has_many :proposals, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author + has_many :people_proposals, -> { with_hidden }, foreign_key: :author_id, inverse_of: :author has_many :activities - has_many :budget_investments, -> { with_hidden }, foreign_key: :author_id, class_name: "Budget::Investment" + has_many :budget_investments, -> { with_hidden }, + class_name: "Budget::Investment", + foreign_key: :author_id, + inverse_of: :author has_many :budget_investment_change_logs, foreign_key: :author_id, inverse_of: :author, class_name: "Budget::Investment::ChangeLog" - has_many :comments, -> { with_hidden } + has_many :comments, -> { with_hidden }, inverse_of: :user has_many :failed_census_calls has_many :notifications - has_many :direct_messages_sent, class_name: "DirectMessage", foreign_key: :sender_id - has_many :direct_messages_received, class_name: "DirectMessage", foreign_key: :receiver_id + has_many :direct_messages_sent, + class_name: "DirectMessage", + foreign_key: :sender_id, + inverse_of: :sender + has_many :direct_messages_received, + class_name: "DirectMessage", + foreign_key: :receiver_id, + inverse_of: :receiver has_many :legislation_answers, class_name: "Legislation::Answer", dependent: :destroy, inverse_of: :user has_many :follows has_many :budget_rol_assignments diff --git a/app/models/votation_set_answer.rb b/app/models/votation_set_answer.rb index fd4b07d09..c299881b3 100644 --- a/app/models/votation_set_answer.rb +++ b/app/models/votation_set_answer.rb @@ -1,6 +1,6 @@ class VotationSetAnswer < ApplicationRecord belongs_to :votation_type - belongs_to :author, -> { with_hidden }, class_name: "User" + belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :votation_set_answers scope :by_author, -> (author) { where(author: author) } end diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb index d433278a9..64a840231 100644 --- a/app/models/widget/card.rb +++ b/app/models/widget/card.rb @@ -1,6 +1,9 @@ class Widget::Card < ApplicationRecord include Imageable - belongs_to :page, class_name: "SiteCustomization::Page", foreign_key: "site_customization_page_id" + belongs_to :page, + class_name: "SiteCustomization::Page", + foreign_key: "site_customization_page_id", + inverse_of: :cards # table_name must be set before calls to 'translates' self.table_name = "widget_cards"