0

我的活动记录搜索有问题。我以为我的模型设置正确,但我的加入很差(不确定加入或联合是否是正确的方法?应该不难)。

我有指南为具有 start_dates 的旅行创建出价。我想创建一个已过期的出价列表(即开始日期是过去的)。如果出价被拒绝,指南也可以有 LosingBids

在一个完美的世界中,我会有一个结果集,其中包括该指南的失败出价和过期出价,但我发现有 2 个不同的结果集。不幸的是,我无法让任何“过期的投标”发挥作用。代码注释中的结果/错误。

class GuidesController < ApplicationController

  def expired_declined

    #this declined_bids call works
    @declined_bids = LosingBid.where("guide_id = ?", current_guide.id.to_s)

    #this expired_bids call returns Trips, not Bids
    @expired_bids = Bid.where("guide_id = ?", current_guide.id.to_s).expired

    #this expired_bids call gives me the following error:
    #SQLite3::SQLException: no such column: trips.start_date: SELECT  1 AS one FROM #”bids" WHERE (guide_id = '1') AND (trips.start_date < '2018-05-30') LIMIT ?
    @expired_bids = Bid.where("guide_id = ?", current_guide.id.to_s).where("trips.start_date < ?", Date.today)
  end
end

class Guide < ApplicationRecord
  has_many :bids
  has_many :losing_bids
end

class Trip < ApplicationRecord
  has_many :bids
end

class Bid < ApplicationRecord
  belongs_to :trip
  belongs_to :guide

  def self.expired
    Trip.where("start_date <= ?", Date.today) #.where("guide_id = ?", current_guide.id.to_s)
  end
end

class LosingBid < ApplicationRecord
  belongs_to :trip
  belongs_to :guide
end
4

2 回答 2

0

Trip.where("start_date <= ?", Date.today).bids将退还您过期的出价。

您应该expired在 Trip 中而不是 Bid 中移动范围。

如果您想要投标范围,您可以定义。

class Bid
  scope :expired, -> { joins(:trip).where('trips.start_date <= ?', Date.current) }
end
于 2018-05-30T21:49:59.217 回答
0

我真的会质疑您是否需要一个单独的LosingBid模型,或者它是否只是造成重复和不必要的复杂性。相反,只需在包含状态的投标中添加一个枚举列:

class Bid
  enum status: [:pending, :declined, :expired, :accepted]
end

这只是一个简单的整数列,用作位掩码。

这只会让您通过以下方式查询:

Bid.pending
Bid.expired
Bid.where(status: [:pending, :accepted])
Bid.where.not(status: :accepted)

您可以通过以下方式简单地拒绝出价:

class BidsController
  # PATCH /bids/decline
  def reject
    @bid.declined!
    redirect_to bid, notice: 'Bid declined'
  end
end

然后,您可以设置每天运行一次的计划任务以自动使任务过期(例如,使用when gem):

every 1.days do
  runner "BidExpiryService.perform"
end

# app/services/bid_expiry_service.rb
module BidExpiryService
  def self.perform
    bids = Bid.pending
              .joins(:trip)
              .where('trips.start_date <= ?', Date.current)
    bids.update_all(status: Bid.statuses[:expired])
    # @todo notify guides that bid has expired
  end
end
于 2018-05-31T04:08:12.400 回答