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

only() criteria prevents scopes with arguments.

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

      Take this scenario

      Consider the following model that is used to collect User rating on a web item.

      class Rating
      include Mongoid::Document

      field :comment, :type => String
      field :value, :type => Integer
      field :email, :type => String
      field :other
      field :data

      scope :rated_a, lambda

      {|val| where(:value => val) }

      end

      Now if you try to access the scope, after you specify only() criteria, you are no longer able to use a scope with an argument.

      Rating.only(:_id, :value, :comment, :email).where(:email => 'joe@somewhere.com').rated_a(5)

      This will return with:

      ArgumentError: wrong number of arguments (1 for 0)

      But this works:

      Rating.where(:email => 'joe@somewhere.com').rated_a(5)

      As well as this:

      Rating.where(:email => 'joe@somewhere.com').rated_a(5).only(:_id, :value, :comment, :email)

            Assignee:
            Unassigned Unassigned
            Reporter:
            squarelover Sean Wolfe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: