简体   繁体   中英

Ransack sort link for has many association

Rails 4.2.4, Ruby 2.2.3, Ransack 1.7.0

I have a model as:

class Patient < ActiveRecord::Base
  has_many :patient_statuses

   def latest_status
     self.patient_statuses.last.status
   end
end

Another model as:

class PatientStatus < ActiveRecord::Base
  belongs_to :patient

  enum status: [:registered, :pending, :check_up, :admitted, :discharged]
end

I have PatientsController#index action as

def index
    @query = Patient.ransack(params[:q])
    @patients = @query.result
end

How can i sort patients by latest_status ?

As like

sort_link @query, :package_latest_status, 'Status' # index.html.erb

Not sure if it works but have you tried ?

Model.status.find_each

eg

Patient.registered.find_each
Patient.pending.find_each

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