【问题标题】:Check availability of object with bookings in Rails通过 Rails 中的预订检查对象的可用性
【发布时间】:2021-06-14 10:09:43
【问题描述】:

我想通过她的预订检查我的优惠的可用性,

我试过这个范围:

我的模特:

scope :booking_available, -> (arrival_date, departure_date) {
 joins(:bookings).where.not('bookings.arrival_date <= ? AND bookings.departure_date >= ? AND bookings.status != ?', arrival_date, departure_date, 0)
}

但我的搜索结果中的所有优惠都与检查的预订数量重复,

我尝试在范围末尾添加.distinct,但是当我使用其他搜索参数时,我收到错误PG::UndefinedFunction: ERROR: could not identify an equality operator for type json,类似于this error

我想知道我的可用性参数如何与其他搜索参数一起使用?

我的控制器:

if offer_params[:arrival_date].present? && offer_params[:departure_date].present?
  @offers = @offers.booking_available(offer_params[:arrival_date], offer_params[:departure_date])
end

@offers = @offers
      .page(params[:page])
      .per(60)

respond_to do |format|
  format.json { render template: 'offers/v2/search', status: :ok }
end

在我看来我如何呈现优惠:

json[:offers] = @offers.map do |offer|

end

【问题讨论】:

  • 不是很清楚你真正的问题是什么。是how my availability param can work with other search params ? 还是I tried to add .distinct at the end of the scope but when i use other search params i got error
  • 我的问题是how my availability param can work with other search params ?
  • 您能举例说明您的搜索参数是什么意思吗?

标签: ruby-on-rails ruby postgresql


【解决方案1】:

听起来问题在于您的 joins 返回重复记录。在 Rails 中,joins 为关联中的每对匹配记录返回一个单独的模型实例。

在您的示波器上,尝试使用 includes(:bookings).references(:bookings),而不是 joins(:bookings)。这可以防止查询返回重复记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-18
    相关资源
    最近更新 更多