我的ActiveRecord查询是Note.joins(:user).where(category: "Actions")
,生成的SQL如下:SELECT notes.* FROM notes INNER JOIN users ON users.id = notes.user_id WHERE notes.category = 'Actions'
我想要从用户表获取数据而不是笔记表,也就是说,我想要生成如下SQL:SELECT users.* FROM users INNER JOIN notes ON users.id = notes.user_id WHERE notes.category = 'Actions';
那么对应的Active Record查询语句应该是什么呢?
如果我写User.joins(:notes).where(category: "Actions")
,则会抛出错误Mysql2::Error: Unknown column 'users.category'
,因为category属性属于notes表而不是users表。
Note.where(notes: {feeling_score: 7})
将始终返回SELECT notes.* FROM notes WHERE notes.feeling_score = 7
。我该如何在where子句中传递条件运算符,如<
、<=
? - Bloomberg