sort_link asc desc not working for associated relation

Viewed 31

I have a method full_name in owner.rb, I want to sort owner asc and desc in properties index, either it does asc or will do desc, but in this case, I have tried many ways to sort this but none of this is working, I'm quite new using ransack, can anyone please let me how to make this work with associated relations, here property belongs to owner and owner has many properties.

  def full_name
    [first_name,last_name].join(" ").squish
  end

which I am calling in properties index

  td.special-td
  - unless property.owner.nil?
     a href=manager_user_owner_petty_cash_path(property.owner.id) #{property.owner.full_name}

I am trying to apply sort_link on the table column:

 th = sort_link(@q, :owners_full_name, "Landlord")

while this sorting is not working here is my properties index controller

 def index
authorize Property

if !current_user.manager?
  unit_ids = current_user.unit_ids
  total_units_limit = " units.id in (#{unit_ids.join(',')}) or properties.user_id =  #{current_user.id}  "
  properties_ids = company.properties.joins(:units).where(total_units_limit).group(:id).pluck(:id).join(",")
  if properties_ids.blank?
    properties_ids = 0
  end
  owners_limit = " properties.id in (#{properties_ids}) "
end

pagination_limit_check

@q = company.properties.joins("left join units on units.property_id = properties.id ").includes(:units, :owner, :contracts, :tags)
  .where(total_units_limit).ransack(params[:q])
@q.sorts = 'name asc' if @q.sorts.empty?
self.properties = @q.result(distinct: true).page(params[:page]).per(@page_limit)
self.owners = Owner.joins(:properties).where(owners_limit)

end
0 Answers
Related