Last active
August 29, 2015 14:16
-
-
Save ysaito8015/011f629ae0d4bcaf1a50 to your computer and use it in GitHub Desktop.
Heroku で ransack による日付一致検索 ref: http://qiita.com/ysaito8015@github/items/66b1985f695da4efc528
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app[web.1]: Parameters: {"utf8"=>"✓", "q"=>{"order_date_cont"=>"2015-03-02"}} | |
app[web.1]: Purchase Load (4.9ms) SELECT DISTINCT "purchases".* FROM "purchases" WHERE ("purchases"."order_date" ILIKE '%2015-03-02%') ORDER BY "purchases"."order_date" DESC | |
app[web.1]: PG::UndefinedFunction: ERROR: operator does not exist: date ~~* unknown | |
app[web.1]: LINE 1: ... FROM "purchases" WHERE ("purchases"."order_date" ILIKE '%20... | |
app[web.1]: ^ | |
app[web.1]: HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. | |
app[web.1]: SELECT DISTINCT "purchases".* FROM "purchases" WHERE ("purchases"."order_date" ILIKE '%2015-03-02%') ORDER BY "purchases"."order_date" DESC | |
app[web.1]: Rendered shared/_search_result_box.html.erb(6.9ms) | |
app[web.1]: Completed 500 Internal Server Error in 69ms |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private | |
ransacker :order_date do | |
Arel.sql('date(order_date)') | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PurchaseController < ApplicationController | |
private | |
def search_params | |
search_conditions = %i( | |
order_date_date_equals | |
) | |
params.require(:q).permit(search_conditions) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ransack.configure do |config| | |
config.add_predicate 'date_equals', | |
arel_predicate: 'eq', | |
formatter: proc { |v| v.to_date }, | |
formatter: proc { |v| v.present? }, | |
type: :string | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<label>注文日</label> | |
<%= f.search_field :order_date_date_equals, class: 'date' %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment