Reputation: 389
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
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
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