在我的Rails应用中,用户可以参加活动 -
user.rb
我想到了自己错过了一堂课,这堂课讲解如何在类似情况下使用像
有更好的方法吗?
我看了看,这是最接近的,但不太符合需求(我认为)。 如何显示has_many through关系中的唯一记录?
has_many :attendances
has_many :events, through: :attendances
event.rb
has_many :attendances
has_many :users, through: :attendances
...带有出席表,由event_id、user_id和其他一些细节组成 -
attendance.rb
belongs_to :user
belongs_to :writers_event
在寻找特定的考勤记录时,我发现自己通常使用.where ... .first - 例如:
attendance = Attendance.where(user_id: @user.id, event_id: this_event_id).first
我想到了自己错过了一堂课,这堂课讲解如何在类似情况下使用像
find_by
这样的东西——换句话说,当您确信正在查找某个唯一的东西时。也许这并不重要,但是从集合中搜索然后取第一个对象感觉很浪费和错误。有更好的方法吗?
我看了看,这是最接近的,但不太符合需求(我认为)。 如何显示has_many through关系中的唯一记录?