0

I need to sort data in tables with Ransack sort_link method when I use association. I have:

MODELS

class Deal < ActiveRecord::Base
has_many :tasks
...
end

class Task < ActiveRecord::Base
belongs_to  :deal
...
end

CONTROLLER

...
@search = Deal.where(assigned_to: @user_id).where.not(stage: [6, 7]).search(params[:q])
@deals = @search.result.joins(:tasks).where(assigned_to: @user_id).where.not(stage: [6, 7]).uniq
...

_DEALS.HTML.ERB

...
<th style="width: 15%" class="text-center"><%= sort_link @search, 'tasks.due', "Задача", {}, { :remote => true, :method => :get } %></th>
...

The result is that sorting doesn't work, however we have positive signals from server:

SERVER LOG

Processing by DealsController#index as JS
Parameters: {"q"=>{"s"=>"tasks.due desc"}}
Rendered deals/_deals.html.erb (21.0ms)
Rendered deals/index.js.erb (24.8ms)
Completed 200 OK in 39ms (Views: 24.0ms | ActiveRecord: 9.7ms)

Have reviewed dozens of ransack-related issues, including Using Ransack to sort attributes for parent of parent and Rails sorting associations with Ransack but they have no reply so far.

I suspect I'm misdoing something from https://github.com/activerecord-hackery/ransack. Had several smart guesses but unfortunately they didn't work.

THX.

Community
  • 1
  • 1
arthur
  • 348
  • 4
  • 16

1 Answers1

1

Based on github issue, try sort_link @search, 'tasks_due'...

Note that it is assocation_name, not table_name.name as in documentation

mintuhouse
  • 449
  • 5
  • 16