ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Error when sorting parameter comes at the beginning of the hash

Open danilokleber opened this issue 7 years ago • 4 comments

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!

danilokleber avatar Nov 01 '17 23:11 danilokleber

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.

scarroll32 avatar Feb 07 '18 15:02 scarroll32

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?&amp;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.

xandrck avatar Jul 31 '19 09:07 xandrck

Hi @danilokleber did you find the solution for this?

shubham-vinsol avatar Jun 08 '20 20:06 shubham-vinsol

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 😕

danilokleber avatar Jun 09 '20 12:06 danilokleber