0

I want check the availability of my Offer with her bookings,

I tried with this scope:

My model:

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)
}

But i got all offers in my search result duplicate by the number of bookings checked,

I tried to add .distinct at the end of the scope but when i use other search params i got error PG::UndefinedFunction: ERROR: could not identify an equality operator for type json similar to this error

I would like know how my availability param can work with other search params ?

My controller:

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

How i render offers in my view:

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

end
3
  • 1
    not very clear what your real question is. is it how my availability param can work with other search params ? or I tried to add .distinct at the end of the scope but when i use other search params i got error? Commented Jun 17, 2021 at 12:28
  • My question is how my availability param can work with other search params ? Commented Jun 17, 2021 at 14:17
  • can you show the example of what search params did you mean? Commented Jun 17, 2021 at 16:08

1 Answer 1

1
+50

It sounds like the problem is your joins returning duplicate records. In Rails, joins returns a separate instance of the model for each matching pair of records in the association.

On your scope, instead of joins(:bookings), try using includes(:bookings).references(:bookings). This prevents the query from returning duplicate records.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.