使用 Ransack 对父级的父级属性进行排序 [英] Using Ransack to sort attributes for parent of parent
问题描述
我知道如何对父属性使用 Ransack 很棒的 sort_link
方法,但是当我将相同的逻辑外推到祖父母的属性时,它不起作用(我可以单击链接,只是没有排序发生).
I know how to use Ransack's awesome sort_link
method for parent attributes, but when I extrapolate the same logic to the attributes of the grandparent, it's not working (I can click the link, just no sorting happens).
层次结构如下:Signup has_many Request has_many Transaction
.我正在从交易的角度进行搜索.
Hierarchy goes like this: Signup has_many Request has_many Transaction
. I'm searching in from the Transaction perspective.
控制器代码:
def index
@q = Transaction.ransack(params[:q])
@transactions = @q.result.includes(:request => :signup)
end
查看代码:
<th><%= sort_link(@q, 'requests.signup.email', "Email") %></th>
<th><%= sort_link(@q, 'requests.pickupdate', "Pick up date") %></th>
<th><%= sort_link(@q, 'requests.returndate', "Returndate") %></th>
顺便说一句,搜索代码(例如,:request_returndate_eq
)很好,只是排序不起作用.
Incidentally, the search code (e.g., :request_returndate_eq
) is fine, it's just the sorting that's not working.
型号代码:
class Signup < ActiveRecord::Base
has_many :requests
end
class Request < ActiveRecord::Base
belongs_to :signup
has_many :transactions, dependent: :destroy
end
class Transaction < ActiveRecord::Base
belongs_to :request
end
控制台输出
第一次点击以升序排序(尽管任何点击它总是产生 200 响应,即使视图实际上没有改变)
On first click to sort ascending (though with any click it always produces a 200 response, even though the view doesn't actually change)
Started GET "/admin/transactions?q%5Bs%5D=requests.signups.email+asc" for 127.0.0.1 at 2014-07-07 13:40:16 -0700
Processing by TransactionsController#index as HTML
Parameters: {"q"=>{"s"=>"requests.signups.email asc"}}
Rendered layouts/_flash.html.erb (0.6ms)
(0.4ms) SELECT "transactions"."name" FROM "transactions"
Transaction Load (0.6ms) SELECT "transactions".* FROM "transactions"
Request Load (0.3ms) SELECT "requests".* FROM "requests" WHERE "requests"."id" IN (11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50)
Signup Load (0.1ms) SELECT "signups".* FROM "signups" WHERE "signups"."id" IN (4, 5, 1, 2)
Rendered transactions/index.html.erb within layouts/application (74.3ms)
Completed 200 OK in 85ms (Views: 81.7ms | ActiveRecord: 1.4ms)
推荐答案
您可以使用 委托 避免在视图中使用父级或父级表达式.
You can using delegate to avoid parent or parent expression in view.
型号
class Transaction < ActiveRecord::Base
belongs_to :request
delegate :email, to: :request, prefix: true
end
交易视图代码:
<th><%= sort_link(@q, 'request_email', "Email") %></th>
这篇关于使用 Ransack 对父级的父级属性进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!