Rails Check if user id is in array

I am trying to create a condition based on the fact that the "user" is a "member". Basically I need a way to check if current_user.id matches any of the user_id of any members. The broken code that I have now:

<% if current_user = @page.members %>
  you can view this content.
<% end %>

I am looking for something like: "If current_user.id exists in the" user_id "of any members."

+3
source share
4 answers

Something like this, based on the field names in your question:

<% if @page.members.map(&:user_id).include? current_user.id %>
  You can view this content
<% end %>
+12
source

Assuming your variable @page.memberscontains an array, you can use the method include?:

<% if @page.members.include? current_user %>
  you can view this content.
<% end %>

, , , , :

<% if @page.members.include? current_user.id %>
  you can view this content.
<% end %>
+4
@member_ids = @page.members.map{|m| m.id()}

@memeber_ids.include? current_user.id()
+1

, include? -.

, CanCan, "".

CanCan , , ?

  • , .
  • - ( ).
  • CanCan, , , , .
  • .

, .

0
source

All Articles