User
has_many :reservations, :dependent => :destroy
has_many :events, :through => :reservations
Reservation
belongs_to :user
belongs_to :event
Event
has_many :reservations, :dependent => :destroy
has_many :attendees, :through => :reservations, :source => :user
我想要的是一種方法User
,我可以通過查詢找出該用戶在給定事件中的狀態。喜歡的東西:Rails:從has_many中拉取值:通過關係模型
def attendee_status(event)
res = self.reservations.where(event: event.id).first
if res
res.status
else
0
end
end
我很困惑該協會語法,除其他事項外... column reservations.event does not exist
每個用戶應該只對任何特定事件一個預約...... first
假設。在Reservation
模式status
方法應該返回的%w[not_attending, awaiting_payment, payment_received]
一個什麼是「Rails的方式」來處理從模型中,這些關係/看起坐模型(甚至開始用(更好的辦法?)
無論如何,更清晰的方法名稱:attendee_status(event)
)?!?