-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
Say you have a polymorphic setup like the following where all classes involved in the relationship use a custom id field of type String
class Picture include Mongoid::Document field :_id, type: String, pre_processed: true, overwrite: true, default: ->{ BSON::ObjectId.new.to_s } belongs_to :imageable, polymorphic: true end class Employee include Mongoid::Document field :_id, type: String, pre_processed: true, overwrite: true, default: ->{ BSON::ObjectId.new.to_s } has_many :pictures, as: :imageable end
If we create an employee and picture, then link them together so we have:
Employee.first => #<Employee _id: 539f42096c75633c62010000 > Picture.first => #<Picture _id: 539f428b6c75633c62020000, imageable_type: "Employee", imageable_id: "539f42096c75633c62010000"> Picture.first.imageable_id == Employee.first.id => true Picture.first.imageable_id.class => String
Then try to access the employee's pictures via the association, nothing is found - even though there is a picture associated with the employee
Employee.first.pictures
=> [] # this should've returned the first picture
Underneath the covers, the selector tries to find an imageable_id of type BSON::ObjectId, not String.
MOPED: 127.0.0.1:27017 QUERY database=dev collection=pictures selector={"imageable_id"=>BSON::ObjectId('539f42096c75633c62010000'), "imageable_type"=>"Employee"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil
- is related to
-
MONGOID-3606 Bug Fix: Polymorphic foreign keys are not converted to ObjectIds
- Closed
-
MONGOID-3762 Polymorphic association query for inverse relation
- Closed