简体   繁体   中英

How to get result of second table while using joins in rails active record query

My ActiveRecord Query is Note.joins(:user).where(category: "Actions") and its generating sql as SELECT notes.* FROM notes INNER JOIN users ON users.id = notes.user_id WHERE notes.category = 'Actions'

I want to get data from users table not from notes table means i am trying to generate sql as SELECT users.* FROM users INNER JOIN notes ON users.id = notes.user_id WHERE notes.category = 'Actions';

What would be the Active Record query for it.

if i am doing User.joins(:notes).where(category: "Actions") then it throws error Mysql2::Error: Unknown column 'users.category' as category is attribute of notes table not of users table.

尝试这个 -

User.joins(:notes).where(notes: {category: "Actions"})

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