Pre*_*rem 2 ruby mysql ruby-on-rails ruby-on-rails-4
我的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'
我想从用户表中获取数据而不是从notes表中获取数据意味着我正在尝试生成sql as 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'因为类别是注释表的属性而不是用户表.
试试这个 -
User.joins(:notes).where(notes: {category: "Actions"})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |