From 090bc0226925ea0bd64faef6450f65893c82ce71 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Thu, 4 Jan 2018 14:55:36 +1300 Subject: [PATCH] `belongs_to` defaults to required. setting some to optional --- app/models/crop.rb | 4 ++-- app/models/harvest.rb | 2 +- app/models/post.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index a961daf58..f87fbb262 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -17,8 +17,8 @@ class Crop < ApplicationRecord has_many :harvests has_many :plant_parts, -> { uniq.reorder("plant_parts.name") }, through: :harvests belongs_to :creator, class_name: 'Member' - belongs_to :requester, class_name: 'Member' - belongs_to :parent, class_name: 'Crop' + belongs_to :requester, class_name: 'Member', optional: true + belongs_to :parent, class_name: 'Crop', optional: true has_many :varieties, class_name: 'Crop', foreign_key: 'parent_id' has_and_belongs_to_many :posts # rubocop:disable Rails/HasAndBelongsToMany diff --git a/app/models/harvest.rb b/app/models/harvest.rb index a5328ef49..a58eaa991 100644 --- a/app/models/harvest.rb +++ b/app/models/harvest.rb @@ -35,7 +35,7 @@ class Harvest < ApplicationRecord belongs_to :crop belongs_to :owner, class_name: 'Member', counter_cache: true belongs_to :plant_part - belongs_to :planting + belongs_to :planting, optional: true ## ## Scopes diff --git a/app/models/post.rb b/app/models/post.rb index ae27dbb50..2dc254741 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -6,7 +6,7 @@ class Post < ApplicationRecord # # Relationships belongs_to :author, class_name: 'Member' - belongs_to :forum + belongs_to :forum, optional: true has_many :comments, dependent: :destroy has_and_belongs_to_many :crops # rubocop:disable Rails/HasAndBelongsToMany # also has_many notifications, but kinda meaningless to get at them