Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2493

Polymorphic, single collection inheritance, model_name

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.10
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Hi,

      I am having trouble with mongoid v3 since I have updated from v2. The issue is with my existing polymorphic associations.

      I have a class "Listing" which is the parent to a lot of other classes in my application. I also have a comments model which is related via polymorphic association.

      Listing has_many :comments, as: :commentable.

      I have setup the sub classes of Listing to use "Listing" as it's model_name because I want them all to be accessed through one controller, and routes. Example: Listings::Vehicle.model_name == "Listing" Route: /listings/<vehicle's bson id> will access the ListingsController, which will in turn pull the Vehicle listing from the database. All of this works fine still.

      What doesn't work now is accessing comments. In version 2 of mongoid it would use "Listing" as the commentable_type while trying to retrieve the comments from the database. Version 3, now uses the sub class name (Listings::Vehicle) as the commentable_type field. This of course doesn't retrieve any comments because all the exiting comments in the database have the commentable_type set to "Listing"

      What can I do to ensure the same functionality I was using in Version 2 of Mongoid?

            Assignee:
            Unassigned Unassigned
            Reporter:
            demersus demersus
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: