Commit ed7793d4 by Patrick Koperwas Committed by John Hawthorn

Create Failing Test

Restoring polymorphic has_one relationships errored because paranoia was
not correctly looking up the foreign_key.

Output from failing test -

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: parent_model_id: SELECT  "polymorphic_models".* FROM "polymorphic_models" WHERE ("polymorphic_models"."deleted_at" IS NOT NULL) AND (parent_model_id)  ORDER BY "polymorphic_models"."id" ASC LIMIT 1

The test sets up a PolymorphicModel, which has_many parents. The
ParentModel then has a has_one relationship with PolymorphicModel. When
restoring, the foreign key is set as - `self.class.name.to_s.underscore_id`
which will be parent_model_id, instead of the :as option.
parent 9595a2c2
......@@ -33,6 +33,7 @@ def setup!
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_column_models (id INTEGER NOT NULL PRIMARY KEY, destroyed_at DATETIME)'
ActiveRecord::Base.connection.execute 'CREATE TABLE custom_sentinel_models (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME NOT NULL)'
ActiveRecord::Base.connection.execute 'CREATE TABLE non_paranoid_models (id INTEGER NOT NULL PRIMARY KEY, parent_model_id INTEGER)'
ActiveRecord::Base.connection.execute 'CREATE TABLE polymorphic_models (id INTEGER NOT NULL PRIMARY KEY, parent_id INTEGER, parent_type STRING, deleted_at DATETIME)'
end
class WithDifferentConnection < ActiveRecord::Base
......@@ -657,6 +658,19 @@ class ParanoiaTest < test_framework
setup!
end
def test_restore_recursive_on_polymorphic_has_one_association
parent = ParentModel.create
polymorphic = PolymorphicModel.create(parent: parent)
parent.destroy
assert_equal 0, polymorphic.class.count
parent.restore(recursive: true)
assert_equal 1, polymorphic.class.count
end
private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
......@@ -709,6 +723,7 @@ class ParentModel < ActiveRecord::Base
has_many :very_related_models, :class_name => 'RelatedModel', dependent: :destroy
has_many :non_paranoid_models, dependent: :destroy
has_many :asplode_models, dependent: :destroy
has_one :polymorphic_model, as: :parent, dependent: :destroy
end
class RelatedModel < ActiveRecord::Base
......@@ -798,3 +813,8 @@ end
class NoConnectionModel < ActiveRecord::Base
end
class PolymorphicModel < ActiveRecord::Base
acts_as_paranoid
belongs_to :parent, polymorphic: true
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment