过滤活动记录关系

时间:2015-11-10 12:51:00

标签: sql ruby-on-rails ruby postgresql ruby-on-rails-4

我有一个可用性类

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),然后才能预订空房。

  

例如:只要我们有48个,您就只能预订可用性   从开始时间开始的小时通知

我尝试通过添加新范围来实现这一目标:

scope :after_notice_time, -> {joins{venue}.where{start_time >= (venue.notice_time.hours.from_now)}}

但是错误

  

[58] pry(main)&gt; Availability.after_notice_time
  可用性负载(4.9ms)选择“可用性”。* FROM“可用性”INNER JOIN   “设施”ON“设施”。“id”=“可用性”。“facility_id”   INNER JOIN“场地”ON“场地”。“id”=“设施”。“venue_id”在哪里   “可用性”。“start_time”&gt; =“小时”。“from_now”

     

PG :: UndefinedTable:错误:缺少表的FROM子句条目   “小时”LINE 1:...“venue_id”WHERE“availablebilities”。“start_time”&gt; =   “小时”。“F ...                                                                ^:选择“可用性”。* FROM“可用性”INNER JOIN   “设施”ON“设施”。“id”=“可用性”。“facility_id”   INNER JOIN“场地”ON“场地”。“id”=“设施”。“venue_id”在哪里   “可用性”。“start_time”&gt; =“小时”。“from_now”   =&GT; #&lt;可用性:: ActiveRecord_Relation:0x5b525ac&GT;

如何过滤结果,以便检查每项可用性venue.notice_time,并确保我的最终@search仅包含其start_time大于current time + notice time

的可用性

1 个答案:

答案 0 :(得分:1)

您可以尝试使用纯SQL(此代码是PG特定的):

$return_html.= '<a class="cat_link" href="'.$category_url.'">''<h5 class="header_line ''</a>';