Question

I am using Ransack in my RoR application and so far most of how I have been using it has been simple. But I now have two issues with Ransack that I need help on.

  1. I am trying to search on dates on my holds table. The holds table has two date fields. lowdate and highdate. I have the following jQuery code in my holds.coffee file for the jQuery datepicker to ensure that the date format is in the mm/dd/yy format. The following is an example of the lowdate, but I have another for highdate.

    jQuery -> $(document).on "page:change", -> $("#lowdate").datepicker changeMonth: true changeYear: true dateFormat: "mm/dd/yy" yearRange: "-5:+10"

It seems that the date is being passed in the mm/dd/yy format in the search critera and I think it needs to be in the yy-mm-dd format. Not sure how to do that for searches though.

My ransack search code in my index.html.erb file:

<%= search_form_for @search do |f| %>
  <div class="span2 input">
    <% f.label :holdfrom_gteq %>
    <%= f.text_field :holdfrom_gteq, placeholder: "Low Date", id: "lowdate" %>
  </div>
  <div class="span2 input">
    <% f.label :holdto_lteq %>
    <%= f.text_field :holdto_lteq, placeholder: "High Date", id: "highdate" %>
  </div>
  <div class="span1">
    <%= f.submit "Search", name: nil, class: "btn-small btn-primary"%>
  </div>
<% end %>

And this is in my log file:

Parameters: {"utf8"=>"√", "q"=>{"holdfrom_gteq"=>"05/25/2014", "holdto_lteq"=>"05/31/2014"}}
  1. My next problem involves searching on items that are not in the current table. For example, my hold table has a customer_id linking a hold back to a customer. I want to be able to search on customer_name (field in the customers table) but have no idea how to do that since customer_name is not part of the holds table.

Any help that can be provided to the above two issues would be much appreciated. Still relatively new to rails and still learning. I did watch the railscast #370 for Ransack but it doesn't touch on either of the two items above.

Was it helpful?

Solution

Ok, in the end the answer to this wasn't as difficult as I thought it would be. Since I had a relationship between holds and customer, a customer can have many holds, then all I needed to do was prefix the search variable with the model name.

So, if I wanted to search on customer name I would have the variable something like this:

customer_first_name_cont

Don't recall what the exact issue was with the dates, but they are working fine too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top