如何使用活动记录轨道的where和条件来编写

时间:2015-09-30 05:10:36

标签: ruby-on-rails activerecord

如何使用活动记录轨道上的where和条件来编写此查询

SELECT c.name FROM类别as c join categories_coaches as cc on cc.category_id=c.id where cc.coach_id=16 and c.parent_id=1

1 个答案:

答案 0 :(得分:1)

这应该这样做:

Category.joins(:categories_coaches)
    .where('categories_coaches.id = ? AND categories.parent_id = ?', 16, 1)
    .select('categories.name')