Kevin Brown
Kevin Brown

Reputation: 12650

Rails scoping embedded documents

I'm trying to get a list of upcoming books due by doing this:

scope :upcoming, lambda { where('books.date_due' => {'$lt' => Date.yesterday + 7.days, '$gt' => Date.today - 14.days})

But I need to limit it to the first embedded document...this queries all embedded documents per book...

Do I add an additional query by the created_at field to do this?

Upvotes: 0

Views: 145

Answers (1)

mu is too short
mu is too short

Reputation: 434795

An embeds_many embedded document is really just an array of hashes with some Mongoid wrapping. That means that you can use the usual array notations for looking at specific elements in the array; in particular, you can say:

where('books.0.date_due' => { ... })
#            ^ element zero is the first entry

Upvotes: 1

Related Questions