我在查詢我的預訂模型以獲取詳細信息,其中包括has_many約會列表。複雜查找條件以返回一條記錄
要做到這一點我使用範圍:
scope :current_cart,
Booking.includes(:appointments).where(:parent_id => 1).where(:complete =>nil).order("created_at DESC").limit(1)
然後在視圖:
<% @booking.appointments.each do |appointment| %>
# info output
<% end %>
得到這個工作,在控制器中,我必須這樣做:
@booking = Booking.current_cart[0]
這是我擔心的[0]位。我想我正在使用一個想要返回一個集合的方法,這意味着我必須聲明我想要第一個(唯一)記錄。我如何陳述一個更適合於獲取成員的類似範圍?
這推動我回答謝謝! – Will