Add missing has_many relations for users

Usually when we specify a `belongs_to` relations, we also specify its
equivalent `has_many`. That allows us to write, for example:
`topic.user.topics`.
This commit is contained in:
Javi Martín
2019-10-24 03:50:58 +02:00
parent 915e2792fc
commit 94d2496f8f
13 changed files with 55 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
class Activity < ApplicationRecord
belongs_to :actionable, -> { with_hidden }, polymorphic: true
belongs_to :user, -> { with_hidden }
belongs_to :user, -> { with_hidden }, inverse_of: :activities
VALID_ACTIONS = %w[hide block restore valuate email]

View File

@@ -1,5 +1,9 @@
class Budget::Investment::ChangeLog < ApplicationRecord
belongs_to :author, -> { with_hidden }, class_name: "User", required: false
belongs_to :author, -> { with_hidden },
class_name: "User",
foreign_key: "author_id",
inverse_of: :budget_investment_change_logs,
required: false
validates :old_value, presence: true
validates :new_value, presence: true

View File

@@ -6,7 +6,7 @@ class Legislation::Annotation < ApplicationRecord
serialize :ranges, Array
belongs_to :draft_version, foreign_key: "legislation_draft_version_id"
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_annotations
has_many :comments, as: :commentable, dependent: :destroy
validates :text, presence: true

View File

@@ -20,7 +20,7 @@ class Legislation::Proposal < ApplicationRecord
acts_as_paranoid column: :hidden_at
belongs_to :process, foreign_key: "legislation_process_id"
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_proposals
belongs_to :geozone
has_many :comments, as: :commentable

View File

@@ -6,7 +6,7 @@ class Legislation::Question < ApplicationRecord
translates :title, touch: true
include Globalizable
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :legislation_questions
belongs_to :process, foreign_key: "legislation_process_id"
has_many :question_options, -> { order(:id) }, class_name: "Legislation::QuestionOption", foreign_key: "legislation_question_id",

View File

@@ -28,7 +28,7 @@ class Poll < ApplicationRecord
has_many :ballot_sheets
has_and_belongs_to_many :geozones
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :polls
belongs_to :related, polymorphic: true
belongs_to :budget

View File

@@ -1,6 +1,6 @@
class Poll::Answer < ApplicationRecord
belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_answers
delegate :poll, :poll_id, to: :question

View File

@@ -1,6 +1,6 @@
class Poll::PairAnswer < ApplicationRecord
belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: "User"
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"

View File

@@ -2,7 +2,7 @@ class Poll::PartialResult < ApplicationRecord
VALID_ORIGINS = %w[web booth]
belongs_to :question, -> { with_hidden }
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_partial_results
belongs_to :booth_assignment
belongs_to :officer_assignment

View File

@@ -9,7 +9,7 @@ class Poll::Question < ApplicationRecord
include Globalizable
belongs_to :poll
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_questions
has_many :comments, as: :commentable
has_many :answers, class_name: "Poll::Answer"

View File

@@ -1,7 +1,7 @@
class Poll::Recount < ApplicationRecord
VALID_ORIGINS = %w[web booth letter].freeze
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_recounts
belongs_to :booth_assignment
belongs_to :officer_assignment

View File

@@ -4,7 +4,7 @@ class Topic < ApplicationRecord
include Notifiable
belongs_to :community
belongs_to :author, -> { with_hidden }, class_name: "User"
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :topics
has_many :comments, as: :commentable

View File

@@ -24,7 +24,12 @@ class User < ApplicationRecord
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 :activities
has_many :budget_investments, -> { with_hidden }, foreign_key: :author_id, class_name: "Budget::Investment"
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 :failed_census_calls
has_many :notifications
@@ -33,6 +38,40 @@ class User < ApplicationRecord
has_many :legislation_answers, class_name: "Legislation::Answer", dependent: :destroy, inverse_of: :user
has_many :follows
has_many :budget_rol_assignments
has_many :legislation_annotations,
class_name: "Legislation::Annotation",
foreign_key: :author_id,
inverse_of: :author
has_many :legislation_proposals,
class_name: "Legislation::Proposal",
foreign_key: :author_id,
inverse_of: :author
has_many :legislation_questions,
class_name: "Legislation::Question",
foreign_key: :author_id,
inverse_of: :author
has_many :polls, foreign_key: :author_id, inverse_of: :author
has_many :poll_answers,
class_name: "Poll::Answer",
foreign_key: :author_id,
inverse_of: :author
has_many :poll_pair_answers,
class_name: "Poll::PairAnswer",
foreign_key: :author_id,
inverse_of: :author
has_many :poll_partial_results,
class_name: "Poll::PartialResult",
foreign_key: :author_id,
inverse_of: :author
has_many :poll_questions,
class_name: "Poll::Question",
foreign_key: :author_id,
inverse_of: :author
has_many :poll_recounts,
class_name: "Poll::Recount",
foreign_key: :author_id,
inverse_of: :author
has_many :topics, foreign_key: :author_id, inverse_of: :author
has_many :budgets, through: :budget_rol_assignments
has_many :votation_set_answers
belongs_to :geozone