简体   繁体   中英

Ordering on a joint table in rails

I have 3 models/db-rows Rules, Users, and Knowledge (the connection between a Rule and a User, with a score(float))

What is wrong with the following line.

Rule.where(parent_id: 2).joins("LEFT JOIN knowledge ON knowledge.user_id = 2 AND
knowledge.rule_id = rules.id").order("knowledge.score")

I want all the rules from a particular parent sorted, with the goal to find where a particular user has the lowest knowledge score.

It should be 'knowledges' not 'knowledge' if you're following rails convention. Or you have to change table_name in your Knowledge model to 'knowledge'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM