Ransackで締め切り日と現在の日付との差によって絞り込みをしたくスコープを定義しました。
(3日で締め切り、7日で締め切りなど)
それをリンクをクリックするとその条件にあったものだけが表示されてほしいのですが
SELECT *になってしまい、全件表示されてしまいます。
link_toにはスコープはつかうことはできないのでしょうか?
アドバイスいただけたら幸いです。
ruby
1# 引数を入れたスコープ 2 scope :day_lefts, -> (day){ where("deadline < ?", Time.zone.today + (day).days)} 3 # 残り3日 4 scope :limit_three_days, -> { where(deadline: Time.zone.today + 3.days )} 5 # 残り7日 6 scope :seven_days, -> { where("deadline < ?", Time.zone.today + 7.days )} 7 scope :limit_seven_days, -> { where(deadline: Time.zone.today + 7.days )} 8 # 残り14日 9 scope :two_weeks, -> { where("deadline < ?", Time.zone.today + 14.days )} 10 scope :limit_two_weeks, -> { where(deadline: Time.zone.today + 14.days )} 11 # 残り31日 12 scope :one_month, -> { where("deadline < ?", Time.zone.today + 31.days )} 13 scope :limit_one_month, -> { where(deadline: Time.zone.today + 31.days )} 14 # Ex:- scope :active, -> {where(:active => true)} 15 def self.ransackable_scopes(auth_object = nil) 16 # %i(day_left seven_days) 17 %i(day_left day_lefts limit_three_days seven_days limit_seven_days two_weeks limit_two_weeks one_month limit_one_month) 18 end
ruby
1%= link_to "12日以内", search_prizes_path(@q, :'q[day_left_gteq' => '1', :'q[day_left_lteq' => '12')%> 2<%= link_to "7日以内", search_prizes_path(:limit_seven_days ) %> 3<%= link_to "7日以内", search_prizes_path(:seven_day )%> 4<%= link_to "31日以内", search_prizes_path(@q, :'q[:one_month]') %>
コントローラー
ruby
1class PrizesController < ApplicationController 2 before_action :set_q, only: [:index, :search] 3 4 def index 5 @prizes = @q.result(distinct: true) 6 end 7 8 def show 9 @prize = Prize.find(params[:id]) 10 end 11 12 def new 13 @categories = Category.all 14 @prize_sellers = PrizeSeller.all 15 @prize = Prize.new 16 end 17 18 def create 19 @prize = Prize.new(prize_params) 20 @prize.save 21 end 22 23 def edit 24 @prize = Prize.find(params[:id]) 25 end 26 27 def update 28 @prize = Prize.find(params[:id]) 29 end 30 31 def search 32 33 @results = @q.result 34 end 35 36 def destroy 37 prize = Prize.find(params[:id]) 38 prize.destroy! 39 end 40 41 42 43 private 44 def prize_params 45 params.require(:prize).permit(:name, :name_kana, :introduction, :caution, :image, :prefecture_id, :number_of_winner, 46 :start_date, :deadline, :category_id, :prize_seller_id) 47 end 48 49 def set_q 50 @q = Prize.ransack(params[:q]) 51 52 end 53end
controllerはどう書きましたか
ありがとうございます。
controller情報追記しました。
他のリンクもSELEDT *になっているのでコントローラーが間違っているのかもしれません。
以前実装したときと同じような書き方はしているのですが。。。
Started GET "/prizes/search?q%5Bnumber_of_win_gteq=1&q%5Bnumber_of_win_lteq=2" for ::1 at 2022-01-18 19:54:18 +0900
Processing by PrizesController#search as HTML
Parameters: {"q"=>{"number_of_win_gteq"=>"1", "number_of_win_lteq"=>"2"}}
Rendering layout layouts/application.html.erb
Rendering prizes/search.html.erb within layouts/application
Rendered layouts/_header.html.erb (Duration: 2.7ms | Allocations: 1285)
Prize Load (0.2ms) SELECT "prizes".* FROM "prizes"
↳ app/views/prizes/search.html.erb:8
Rendered layouts/_notification.html.erb (Duration: 0.2ms | Allocations: 37)
Rendered layouts/_prize-navi.html.erb (Duration: 1.1ms | Allocations: 134)
Rendered prizes/search.html.erb within layouts/application (Duration: 22.7ms | Allocations: 12156)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layout layouts/application.html.erb (Duration: 37.3ms | Allocations: 18247)
Completed 200 OK in 75ms (Views: 73.3ms | ActiveRecord: 0.2ms | Allocations: 18881)
全件取得してしまっています。
number_of_win というのは Prize のスキーマですか?
すいません。number_of_winのところはただのタイポでした。
回答2件
あなたの回答
tips
プレビュー