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

Fix restore with polymorphic has_one relationships #189

Closed
wants to merge 2 commits into from
Closed

Fix restore with polymorphic has_one relationships #189

wants to merge 2 commits into from

Conversation

PatKoperwas
Copy link
Contributor

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 correct :as option.

To fix:

If the association object is a has_one relationship with an :as option,
it will have a type attribute (see
https://github.com/rails/docrails/blob/master/activerecord/lib/active_record/reflection.rb#L280).

If this type attributes is present, it will be the type column on the
polymorphic model. The foreign key can be found as an attribute on
the association object.

This should fix #174. Solution is based off of @hamidumi 's suggestions in #174.

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.
If the association object is a has_one relationship with an :as option,
it will have a type attribute (see
https://github.com/rails/docrails/blob/master/activerecord/lib/active_record/reflection.rb#L280).

If this type attributes is present, that will be the type column on the
polymorphic model. The foreign key can be found as an attribute on
the association object.
johnnylai added a commit to johnnylai/paranoia that referenced this pull request Dec 19, 2014
@sudara
Copy link

sudara commented Jan 7, 2015

Let me know if there's anything I can do to help get this beauty merged! :)

@emilyadavis303
Copy link

Any word on when this might get merged in?

@jhawthorn jhawthorn added this to the 2.0.5 milestone Jan 22, 2015
@jhawthorn
Copy link
Collaborator

This was merged. Thanks very much. 🍰

@jhawthorn jhawthorn closed this Jan 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants