【问题标题】:How to check current_user is present in particular recordset?如何检查 current_user 是否存在于特定记录集中?
【发布时间】:2015-03-25 12:53:34
【问题描述】:

我有三个表 User、Event 和 EventInvitee。 用户创建一个事件并邀请该事件中的其他用户。 现在使用下面的代码,我可以在特定事件中邀请所有用户。

@event=Event.find(4)
 @event.event_invitees

但现在我想检查@event.event_invitees 中是否存在 current_user?

我该怎么做?

EventInvitee 表的字段是 id、user_id、event_id,并且这三个表之间的关系定义正确。

请帮帮我。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可以检查current_user 是否存在于选定的EventInvite 的使用exists? 方法中:

    @event.event_invitees.exists?(user_id: current_user.id)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 2010-09-07
      • 2012-06-28
      • 1970-01-01
      • 2015-07-17
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多