Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/crop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ def all_companions
(companions + parent.companions).uniq
end

before_destroy :destroy_reverse_companionships

private

def destroy_reverse_companionships
CropCompanion.where(crop_b: self).destroy_all
end

def count_uses_of_property(col_name)
plantings.unscoped
.where(crop_id: id)
Expand Down
14 changes: 14 additions & 0 deletions spec/models/crop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,20 @@
end
end

context "destroying a crop" do
let!(:crop_a) { FactoryBot.create(:crop) }
let!(:crop_b) { FactoryBot.create(:crop) }

before do
CropCompanion.create(crop_a: crop_a, crop_b: crop_b)
CropCompanion.create(crop_a: crop_b, crop_b: crop_a)
end

it "destroys companion links" do
expect { crop_a.destroy }.to change { CropCompanion.count }.from(2).to(0)
end
end

context "crop rejections" do
let!(:rejected_reason) do
FactoryBot.create(:crop, name: 'tomato',
Expand Down
Loading