Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Declare type key on polymorphic belongs_to #414

Merged
merged 2 commits into from

3 participants

@md5
md5 commented

When a belongs_to association is marked as :polymorphic => true, add a model key of type String for the "type_key_name" field in addition to the key of type ObjectId for "foreign_key"

dillonm-digisynd and others added some commits
@dillonm-digisynd dillonm-digisynd Declare type key on polymorphic belongs_to
When a belongs_to association is marked as :polymorphic => true, a model
key of type String for the "type_key_name" field in addition to the key
of type ObjectId for "foreign_key"
a71eda6
@md5 md5 Fix type of type_key_name field to be String c8105c6
@brianhempel

@md5 @dillonm-digisynd Is there a specific issue you had that's solved by adding the type_key explicitly?

@dillonm-digisynd

One of our projects uses a validation to ensure that only declared keys end up getting saved. In the case of these polymorphic mappings, the type key ends up undeclared. We've worked around it with a plugin, so we can live without the merge, but it makes sense to me to either declare both id and type keys or declare neither.

@brianhempel brianhempel merged commit 28d005e into mongomapper:master
@brianhempel

Agreed, it makes sense. Thanks for the explanation. Merged.

@md5 md5 deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 7, 2012
  1. @dillonm-digisynd

    Declare type key on polymorphic belongs_to

    dillonm-digisynd authored
    When a belongs_to association is marked as :polymorphic => true, a model
    key of type String for the "type_key_name" field in addition to the key
    of type ObjectId for "foreign_key"
Commits on May 8, 2012
  1. @md5
This page is out of date. Refresh to see the latest.
View
1  lib/mongo_mapper/plugins/associations/belongs_to_association.rb
@@ -17,6 +17,7 @@ def proxy_class
def setup(model)
model.key foreign_key, ObjectId unless model.key?(foreign_key)
+ model.key type_key_name, String unless model.key?(type_key_name) if polymorphic?
super
add_touch_callbacks if touch?
end
View
4 test/functional/associations/test_many_documents_as_proxy.rb
@@ -13,7 +13,9 @@ def setup
should "add type and id key to polymorphic class base" do
PostComment.keys.keys.should include('commentable_type')
+ PostComment.keys['commentable_type'].type.should == String
PostComment.keys.keys.should include('commentable_id')
+ PostComment.keys['commentable_id'].type.should == ObjectId
end
should "allow adding to association like it was an array" do
@@ -226,4 +228,4 @@ def setup
end
end
end
-end
+end
Something went wrong with that request. Please try again.