Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incorrect_dependent_option: Fix infinite recursion when association of the same class as model class #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def detect
[[association.klass], nil]
end

deletable_models, destroyable_models = associated_models.partition { |klass| deletable?(klass) }
deletable_models, destroyable_models = associated_models.partition { |klass| deletable?(klass, []) }

case association.options[:dependent]
when :destroy_async
Expand Down Expand Up @@ -147,14 +147,16 @@ def models_having_association_with_options(as:)
end
end

def deletable?(model)
def deletable?(model, processing_list)
return true if processing_list.include?(model)

!defines_destroy_callbacks?(model) &&
dependent_models(model).all? do |dependent_model|
foreign_key = foreign_key(dependent_model.table_name, model.table_name)

foreign_key.nil? ||
foreign_key.on_delete == :nullify || (
foreign_key.on_delete == :cascade && deletable?(dependent_model)
foreign_key.on_delete == :cascade && deletable?(dependent_model, processing_list + [model])
)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ def test_cascade_foreign_key_and_no_callbacks_on_second_level_association
refute_problems
end

def test_cascade_and_same_class_association
Context.create_table(:users) do |t|
t.references :parent, foreign_key: { to_table: :users, on_delete: :cascade }
end.define_model do
has_many :children, class_name: "Context::User", foreign_key: :parent_id
end

refute_problems
end

def test_no_dependent_suggests_nothing
Context.create_table(:companies) do
end.define_model do
Expand Down
Loading