rails mongoid criteria find by association

后端 未结 2 1025
滥情空心
滥情空心 2020-12-28 19:38

I\'m trying to find a record by associated username which is included in a belongs_to relation, but it\'s not working.

Articles belong to Users Users have many artic

2条回答
  •  半阙折子戏
    2020-12-28 20:30

    You can make it little shorter from what rubish suggested:

    user_ids = User.where(username: 'erebus').pluck(:id)
    articles = Article.where(:user_id.in => user_ids)
    

    Or one liner:

    articles = Article.where(:user_id.in => User.where(username: 'erebus').pluck(:id))
    

提交回复
热议问题