我們有一個名為的表,Client并且客戶端以兩種方式屬于一個用戶,a clientcan have anupdater和 has a creator。它也屬于company.
belongs_to :creator, class_name: 'User'
belongs_to :updater, class_name: 'User'
我正在嘗試為company他們所屬的當前父物件獲取所有客戶端,然后按他們的名稱按字母順序排列creator
這是我目前嘗試過的
current_company.clients.includes(:creator).order("creator.name #{sort_dir}")
如果我放入:user包含它會拋出一個錯誤,即用戶不在桌子上。
Association named 'user' was not found on Client;
這是我在網上找到的最接近的。
uj5u.com熱心網友回復:
查看查詢的最簡單方法是呼叫.to_sql.
您將看到,在join執行時,它使用表名users,而不是關系名creator。
這應該有效:
current_company.clients.includes(:creator).order("users.name #{sort_dir}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358373.html
標籤:mysql 红宝石轨道 ruby-on-rails-4
