Rails Console queries working; but the same request in the app produces an error
<p>Im trying to figure out how to make a simple app in Rails 4.</p>

<p>I have models for User, Profile and Identities.  The associations are:</p>

<p>User</p>

<pre><code>has_many :identities, dependent: :destroy

has_one :profile, dependent: :destroy
</code></pre>

<p>Profile</p>

<pre><code>belongs_to :user
</code></pre>

<p>Identity</p>

<pre><code>belongs_to :user
</code></pre>

<p>In the console I can write:</p>

<p>p = Profile.last
p.user.identities.map(&amp;:provider).include?('facebook')</p>

<p>and it correctly produces the result of :false</p>

<p>When I try to write the following in a profiles helper method:</p>

<pre><code>if @profile.user.identities.map(&amp;:provider).include?('facebook')
</code></pre>

<p>I get an error that says:</p>

<pre><code>undefined method `user' for nil:NilClass
</code></pre>

<p>I have a logged in user in the app. I can't understand why I can successfully ask for this in the console, but I get an error when I try it in the app.</p>

<p>Can anyone see what has gone wrong?</p>