我有一個有效性的類過濾活動記錄關係
class Availability < ActiveRecord::Base
# Associations
belongs_to :facility
has_one :venue, through: :facility
end
那堂課我有一個搜索方法,我在我的控制器調用這樣的:
@search = Availability.search(params, get_location)
def self.search(params, location)
perform_search(location, params[:activity], params[:start_time], params[:end_time],params[:period])
end
def self.perform_search(location, activity, start_time, end_time, period)
self
.not_booked
.after_now
.close_to(Venue.close_to(location))
.activity(Activity.find_by_name activity)
.start_date(validate_start_time start_time)
.end_date(validate_end_time end_time)
.within_segment_of_day(period)
.sorted
end
問題:我現在需要添加一個額外的過濾器。每個場地都有自己的最短時間(notice_time:integer type
),然後才能預訂可用性。
For example: You can only book an availability as long as we have 48 hour notice from the start time
我試圖通過添加新的範圍,以實現這一點:
scope :after_notice_time, -> {joins{venue}.where{start_time >= (venue.notice_time.hours.from_now)}}
,但得到錯誤:
[58] pry(main)> Availability.after_notice_time
Availability Load (4.9ms) SELECT "availabilities".* FROM "availabilities" INNER JOIN "facilities" ON "facilities"."id" = "availabilities"."facility_id" INNER JOIN "venues" ON "venues"."id" = "facilities"."venue_id" WHERE "availabilities"."start_time" >= "hours"."from_now"PG::UndefinedTable: ERROR: missing FROM-clause entry for table "hours" LINE 1: ..."venue_id" WHERE "availabilities"."start_time" >= "hours"."f... ^: SELECT "availabilities".* FROM "availabilities" INNER JOIN "facilities" ON "facilities"."id" = "availabilities"."facility_id" INNER JOIN "venues" ON "venues"."id" = "facilities"."venue_id" WHERE "availabilities"."start_time" >= "hours"."from_now" => # < Availability::ActiveRecord_Relation:0x5b525ac>
如何篩選結果,這樣我可以檢查每個可用性venue.notice_time
,並確保我的最終@search
僅包含start_time大於01的可用性
有人給了我這個解決方案,它是幾乎等同於你:'加入(:場地)。 where(「start_time> = now()+ venues.notice_time *'1 hour':: interval」)'這似乎是完美的 – Batman