根据关联表

时间:2015-10-08 11:21:27

标签: ruby-on-rails activerecord

我的rails应用程序ConfirmRequest.rb和Booking.rb中有两个模型。 这些模型通过这种配置相互关联:

class ConfirmRequest < ActiveRecord::Base       
    has_one :booking
end


class Booking < ActiveRecord::Base
    belongs_to :confirm_request
end

所以,我想首先定义2个变量,其中包含已为其创建预订的confirm_requests以及尚未创建预订的其他变量如何在rails中定义这些变量?

2 个答案:

答案 0 :(得分:1)

尝试使用以下查询:

ConfirmRequest.where(id: Booking.pluck(:confirm_request_id))
ConfirmRequest.where.not(id: Booking.pluck(:confirm_request_id))

答案 1 :(得分:0)

类变量中的变量?范围可以为您提供可以创建使用该范围的类变量的数据(虽然不是必需的,范围足够IMO)。如果要在ConfirmRequest中定义范围以返回已创建预订的记录?在那种情况下

scope :has_booking, where("booking_id IS NOT NULL) 

对于未创建的预订

scope :has_no_booking, where(:booking_id => nil) 
相关问题