开发者

MongoID query for a collection that references a collection that references a collection

开发者 https://www.devze.com 2023-02-09 11:27 出处:网络
I have a User model, a Workout model and a Exercise model. I am using Devise for user management and MongoID to interact with MongoDB.

I have a User model, a Workout model and a Exercise model. I am using Devise for user management and MongoID to interact with MongoDB.

User references_many :workouts

Workout references_many :exercises and referenced_in :user

Exercise referenced_in: workout

How do I query the database to give me a list of all the current_user exercises.I have not stored the user.id in the exercise collection, only the workout.id.

Is it possible, or should I rework the model to store the user_id in开发者_开发技巧 both the exercise and workout collections?

Thanks


How about something like this:

workout_ids = Workout.where(:user_id => current_user.id ).all.collect { |w| w.id }

exercises = Exercise.where(:workout_id => { "$in" => workout_ids })

0

精彩评论

暂无评论...
验证码 换一张
取 消