Skip to content
Merged
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
1 change: 1 addition & 0 deletions lib/couchbase-orm/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def destroy_associations!
assoc = self.class.associations
assoc.each do |name, dependent|
next unless dependent
next if dependent == :nullify

model = self.__send__(name)
next unless model.present?
Expand Down
82 changes: 82 additions & 0 deletions spec/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,86 @@ class ObjectN1qlTest < CouchbaseOrm::Base

include_examples('has_many example', context: :n1ql)
end

describe 'dependent: :nullify on has_many' do
context 'with view' do
class RatingNullifyViewTest < CouchbaseOrm::Base
enum rating: [:awesome, :good, :okay, :bad], default: :okay
belongs_to :object_nullify_view_test
view :view_all
end

class ObjectNullifyViewTest < CouchbaseOrm::Base
attribute :name, type: String
has_many :rating_nullify_view_tests, dependent: :nullify
view :view_all
end

before :all do
RatingNullifyViewTest.ensure_design_document!
ObjectNullifyViewTest.ensure_design_document!
RatingNullifyViewTest.delete_all
ObjectNullifyViewTest.delete_all
end

after do
RatingNullifyViewTest.delete_all
ObjectNullifyViewTest.delete_all
end

it 'nullifies the foreign key and does not delete/destroy children' do
obj = ObjectNullifyViewTest.create! name: :bob
r1 = RatingNullifyViewTest.create! rating: :good, object_nullify_view_test: obj
r2 = RatingNullifyViewTest.create! rating: :awesome, object_nullify_view_test: obj

expect { obj.destroy }.to change(ObjectNullifyViewTest, :count).by(-1)

# children still exist
reloaded_r1 = RatingNullifyViewTest.find(r1.id)
reloaded_r2 = RatingNullifyViewTest.find(r2.id)
expect(reloaded_r1).to be_present
expect(reloaded_r2).to be_present
end
end

context 'with n1ql' do
class RatingNullifyN1qlTest < CouchbaseOrm::Base
enum rating: [:awesome, :good, :okay, :bad], default: :okay
belongs_to :object_nullify_n1ql_test
n1ql :n1ql_all
end

class ObjectNullifyN1qlTest < CouchbaseOrm::Base
attribute :name, type: String
has_many :rating_nullify_n1ql_tests, dependent: :nullify, type: :n1ql
n1ql :n1ql_all
end

before :all do
RatingNullifyN1qlTest.ensure_design_document!
ObjectNullifyN1qlTest.ensure_design_document!
RatingNullifyN1qlTest.delete_all
ObjectNullifyN1qlTest.delete_all
end

after do
RatingNullifyN1qlTest.delete_all
ObjectNullifyN1qlTest.delete_all
end

it 'nullifies the foreign key and does not delete/destroy children' do
obj = ObjectNullifyN1qlTest.create! name: :jane
r1 = RatingNullifyN1qlTest.create! rating: :good, object_nullify_n1ql_test: obj
r2 = RatingNullifyN1qlTest.create! rating: :awesome, object_nullify_n1ql_test: obj

expect { obj.destroy }.to change(ObjectNullifyN1qlTest, :count).by(-1)

# children still exist
reloaded_r1 = RatingNullifyN1qlTest.find(r1.id)
reloaded_r2 = RatingNullifyN1qlTest.find(r2.id)
expect(reloaded_r1).to be_present
expect(reloaded_r2).to be_present
end
end
end
end