Permalink
Browse files

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"
  • Loading branch information...
1 parent b965105 commit a71eda6e394c52f0e0584d6562f095d2bc4f8a3d Mike Dillon committed May 7, 2012
@@ -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, ObjectId unless model.key?(type_key_name) if polymorphic?
super
add_touch_callbacks if touch?
end
@@ -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

0 comments on commit a71eda6

Please sign in to comment.