Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Ransack sort_link for non-attributes

I'm using Ransack's sort_link in my rails project to display a list of fees for my payment model. However, 'fee_amount' isn't an attribute of the payment model, but instead a class method of the bill model (which belongs to payment). What I have currently is:

<%= sort_link @search, :bill_fee_amount, "Convenience Fee" %>

which should accesses the bill of the current payment and call the 'fee_amount' method on that bill, which does some calculations and returns a float. It's these floats that I'm hoping to search by.

Can this be done, or can I only sort by attributes of the model I'm dealing with?

like image 855
JCoster22 Avatar asked Oct 01 '12 14:10

JCoster22


1 Answers

You can't with ransack as it comes, but check this issue. It seems some people has made patches for this functionality, but I haven't tried them.

Ransack, internally, it's always using the model as defined in the database, and I couldn't make it use the temporary tables created by the scopes.

PD: reworked the comment as an answer as I always come back to this question :P

like image 59
Mauricio Pasquier Juan Avatar answered Nov 15 '22 08:11

Mauricio Pasquier Juan