scope
を使って絞り込みをしたいのですが、データベース
ではなく、modelに定義したメソッドで絞り込みたい
のですが、記載の仕方が分からずご教示頂けますと幸甚です。
具体的には、以下Tender model
内に記載のtende_status
メソッドを使って、開催中のtenderを絞り込むscope: ongoing, ->
を定義したいです。
scope :ongoing, -> {tender.tender_status == 0}
とか書いてみましたが、これではundefined local variable or method tender
とエラーが出てしまいます。(当然なんでしょうが、、、)
ruby
1# == Schema Information 2# 3# Table name: tenders 4# 5# id :bigint(8) not null, primary key 6# seller_id :bigint(8) not null 7# tender_location_id :bigint(8) not null 8# starts_at :datetime not null 9# ends_at :datetime not null 10# preparation_status :integer default(0), not null 11# created_at :datetime not null 12# updated_at :datetime not null 13# 14 15class Tender < ApplicationRecord 16 scope :open, -> { where(preparation_status:1)} 17 scope :ongoing, -> {tender.tender_status == 0} 18 19 def tender_status 20 time_now = Time.current 21 ### before tender 22 if time_now < starts_at 23 tender_status = 0 24 ### tender ongoing 25 elsif starts_at < time_now && time_now < ends_at 26 tender_status = 1 27 ### tender finished 28 elsif ends_at < time_now 29 tender_status = 2 30 end 31 tender_status 32 end 33end 34
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/02 07:03