【问题标题】:Ransack sorting for Associations doesn't work对关联进行搜查排序不起作用
【发布时间】:2014-10-10 08:05:50
【问题描述】:

当我使用关联时,我需要使用 Ransack sort_link 方法对表中的数据进行排序。 我有:

模型

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

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

控制器

...
@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>
...

结果是排序不起作用,但是我们从服务器收到了积极的信号:

服务器日志

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)

审阅了几十个洗劫相关问题,包括Using Ransack to sort attributes for parent of parentRails sorting associations with Ransack 但他们至今没有回复。

我怀疑我在https://github.com/activerecord-hackery/ransack 做错了什么。有几个聪明的猜测,但不幸的是他们没有工作。

谢谢。

【问题讨论】:

    标签: sorting ransack


    【解决方案1】:

    基于github issue,试试sort_link @search, 'tasks_due'...

    请注意,它是assocation_name,而不是文档中的table_name.name

    【讨论】:

    • ** 谢谢 **
    猜你喜欢
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 2018-12-17
    相关资源
    最近更新 更多