我的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'
因为类别是Notes表的属性而不是Users表的属性。
试试这个 -
User.joins(:notes).where(notes: {category: "Actions"})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句