Umair Ejaz
Umair Ejaz

Reputation: 273

include one attribute of other collection in mongoid rails Query

I want to query with mongoid , I have following models

class Score
   include Mongoid::Document
   field :value, :type => Integer
   belongs_to :user
end


class User
   include Mongoid::Document
   field :name, :type => String
   field :age, :type => Integer
   has_many :scores

I want to query all the scores from collection with their users. but users object should have only have the 'name' field in it

I would be something like

   Score.find.all.includes(:user).only(:name)

Please tell the correct syntax for this

Upvotes: 1

Views: 521

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230551

Score references User, so it is not possible to retrieve them both in one query, because there are no joins in MongoDB.

Either denormalize and include user name in Score or do several queries.

Upvotes: 1

Related Questions