0
User.where(id: users_ids)
.joins('LEFT JOIN names ON names.user_id = users.id AND names.primary = 1')
.joins('LEFT JOIN emails ON emails.user_id = users.id AND emails.primary = 1')
.select('users.*, names.first_name AS f_name , emails.email AS primary_email')
我需要訂購記錄f_name
。當f_name
爲nil
時,記錄將移至最後並通過電子郵件進行排序。Ruby on Rails:空值的訂單記錄
下面是查詢,
User.where(id: users_ids)
.joins('LEFT JOIN names ON names.user_id = users.id AND names.primary = 1')
.joins('LEFT JOIN emails ON emails.user_id = users.id AND emails.primary = 1')
.select('users.*, names.first_name AS f_name , emails.email AS primary_email')
.where('names.first_name IS NOT NULL')
.order('f_name asc').union(
User.where(id: users_ids)
.joins('LEFT JOIN names ON names.user_id = users.id AND names.primary = 1')
.joins('LEFT JOIN emails ON emails.user_id = users.id AND emails.primary = 1')
.select('users.*, names.first_name AS f_name , emails.email AS primary_email')
.where('names.first_name IS NULL')
.order('primary_email asc'))
上面的查詢不起作用。
數據庫 - MySQL的
它是如何不工作? –
對不起,我忘了添加我正在使用分頁'.paginate(page:params [:page])。per_page(10)'並且錯誤'undefined method'paginate'for#' –
不知道Arel :: Nodes :: Union或paginate方法來自何處。可能不是'.union',請嘗試使用'||'或'.or'(如果您使用的是Rails 5) –