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

Query by polymorphic id does not work

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: 4.0.0 final
    • Component/s: None
    • None

      I have the following model:

      class Activity
        belongs_to :source, polymorphic: true
      end
      

      The source can be e.g. a Fund.

      I query activities:
      activities = Activity.all
      Then funds:
      funds = Fund.where(:id.in => activities.pluck(:source_id).compact, name: /something/i)

      The results are non-empty. Then I query:
      Activity.in(source_id: funds.pluck(:id))
      and the result is empty. When queried directly through the mongo console, the expected record is found.

      Some actual results:
      code
      [22] pry(main)> activities.count
      => 10561
      [23] pry(main)> funds.count
      => 1
      [24] pry(main)> Activity.in(source_id: [f.first.id]).count
      => 0
      [25] pry(main)> Activity.in(source_id: [f.first.id])
      => #<Mongoid::Criteria
      selector: {"deleted_at"=>nil, "source_id"=>{"$in"=>[BSON::ObjectId('543d2abf62653157556fd200')]}}
      options: {:sort=>{"date"=>-1}}
      class: Activity
      embedded: false>
      [26] pry(main)> a.pluck(:source_id).include?(f.first.id)
      => false
      [27] pry(main)> a.pluck(:source_id).map(&:to_s).include?(f.first.id.to_s)
      => true
      [29] pry(main)> a.pluck(:source_id).include?(f.first.id.to_s)
      => true
      code

      Seems to be something about the ObjectId conversion?

            Assignee:
            Unassigned Unassigned
            Reporter:
            stanley90x Marek Stanczyk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: