test if record exists in joined table

in my layout i wanna test if the user has a profile so when he clicks profile my app know whether to present him with a show + link to edit the exisiting profile, or just a form for new profile ~ thanks

in my layout i wanna test if the user has a profile so when he clicks profile my app know whether to present him with a show + link to edit the exisiting profile, or just a form for new profile ~ thanks

How do you access the user's profile? If it is @user.profile (or something similar) then @user.profile.nil? should do it.

Colin

yikes, i dunno how @user got instantiated, i forgot to say i’m in a view, i dunno if i’m allowed to do this in a view or not: <% if Profile.find_by_user_id(current_user.id) %>

yikes, i dunno how @user got instantiated, i forgot to say i'm in a view, i dunno if i'm allowed to do this in a view or not: <% if Profile.find_by_user_id(current_user.id) %>

You can do anything you like in a view, but what is wrong with <% if current_user.profile.nil? %>

How many times do I have to ask you not to top post?

Colin

Sorry Colin, It’s the goolge default When you click reply the cursor flashes on a blank line with quoting below, that’s why i always forget but I’ll try to remember from now on Do I have to test for nil like that I’d rather use find or find_by more absolutely ~ thanks (merry christmas)

It is not a matter of how you test for nil, it is a matter of how you access the profile. You suggest using Profile.find_by_user_id(current_user.id) but you can just do current_user.profile. It is the same thing but much easier to understand. You can test for nil any way you like.

Colin

Thanks colin, happy holidays again ha ha Ok so then i’m using if current_user.profile? but the snippet is causing the error application.html.erb:59: syntax error, unexpected keyword_ensure, expecting end-of-input, even though application.html.erb is only 57 lines

<% user_signed_in? %> <% if current_user.profile? %> <%= “profile found” %> <% else %> <%= “profile not found” %> <% end %>

<%= current_user.email %>
  • <%= link_to('Profile', user_profile_path ) %>
  • Account
  • Messages
  • <%= link_to('Sign out', destroy_user_session_path, method: :delete) %>
<% else %>
<%= link_to('User sign in', new_user_session_path) %>
<% end %>

There is no matching 'if' for this 'end'.

Colin

what does this mean without the if <% user_signed_in? %>

what does this mean without the if <% user_signed_in? %>

It is just a statement that evaluates to true or false and does nothing with the result.

Colin

thanks