ransack
ransack copied to clipboard
Error when sorting parameter comes at the beginning of the hash
Hi everyone. First of all thanks for this amazing collaborative work.
I ran into an issue I didn't find documented yet. The problem happens when I try to search and sort at the same time, but it only happens if I sort first. If I search and then sort it works fine. It is possible to be something wrong with my scopes and joins. My example looks like this:
# Model Deal
has_many :deal_events, dependent: :destroy
belongs_to :deal_progress
scope :actives, -> { where(active: true) }
scope :open, -> { where(status: 'open') }
def self.with_pending_events
joins(:deal_events)
.where("deal_events.active = true AND deal_events.done = false")
.distinct
end
# Controller
@deals = Deal.actives.open.with_pending_events.ransack(params[:filter]).result \
.includes(:deal_events, :deal_progress)
If params[:filter]
has the sorting key at the end, everything works fine, for example:
# params[:filter]
{ deal_events_scheduled_to_lteq: '2018-01-01', s: 'deal_progress_value ASC' }
If the problem is within my code, I'm sorry for the noise!
Hi @danilobytes it definitely sounds like a problem, and thanks for reporting it.
Any chance to write some failing tests ? It will help with resolution, a lot.
Hey guys, I've faced same problem. When I'm trying to add some params in search form:
<%= search_form_for @q, url: users_path(status: params[:status]), method: :get do |f| %>
I got right link (with params) in html.
<form class="user_search" id="user_search" action="/users?&status=verified" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
But after submitting search button, I'm loosing all params except search params.
Did some one manage how we can fix it?
Thanks.
Hi @danilokleber did you find the solution for this?
No @shubham-vinsol. I guess I ended up doing some work-around. I can't remember exactly what it was as I don't work at the project this issue was present anymore.
I also didn't find the time to write failing tests to help resolution. Sorry 😕