I have a User model and user_note model. UserNote model having user_id and note_id.
has_one :user_note, dependent: :destroy, required: false
has_one :note, through: :user_note
Having a scope for getting recent_data
scope :recent_data, -> { joins(order_nulls_last) }
While filtering
when 'recent_data'
joins('INNER JOIN user_note ON user_note.user_id = user.user_id
ORDER BY CASE WHEN user_note.imp_date IS NULL THEN 1 ELSE 0 END,
user.last_name ASC, user.first_name ASC')
def self.order_nulls_last
Arel.sql("INNER JOIN user_note ON user_note.user_id =
user.user_id ORDER BY CASE WHEN user_note.imp_date IS NULL THEN 1
ELSE 0 END")
end
In User datatable
def get_raw_records
records = User.all
records = records.includes(:note,:user_note).references(:note,:user_note)
records = records.send(filter) if filter.present? #recent_data
records
end
But hitting from UI I am getting this issue.
ActiveRecord::StatementInvalid (PG::InvalidColumnReference: ERROR: for SELECT
DISTINCT, ORDER BY expressions must appear in select list
LINE 1: ...user_note.user_id = user.user_id ORDER BY CASE WHEN ...
I am trying from last few days but could not be able to fix it. Please help me in fixing this issue.