That dude
That dude

Reputation: 389

how to check if current user have a shop or not

I have a one to one association with shops and user, that means each user can have one shop. What I want to do is check if current user already have a shop or not, if current user has a shop I want to show him link to his shop otherwise a button to add a shop. How can i do this in rails using the if statement.

Upvotes: 0

Views: 82

Answers (3)

Sergio Belevskij
Sergio Belevskij

Reputation: 2947

Possible you would use erb syntax:

<% if current_user&.shop_id %>
  <%= link_to "/shop/#{current_user.shop_id}" %>
<% else %>
  <%= link_to "/add-shop" %>
<% end %>

Upvotes: 0

Mahabub Islam Prio
Mahabub Islam Prio

Reputation: 1085

You can use present? method.

current_user.shop.present?

it returns true if there is any ActiveRecord Data available. please also note the comment on this answer. from @obermillerk

Upvotes: 0

widjajayd
widjajayd

Reputation: 6253

you can use any?

current_user.shop.any?

Returns true if there are any records

Upvotes: 1

Related Questions