Using devise for authentication, undefined method 'size' for nil:NilClass is raised from the following view code:
<% @relationships.each do |relationship| %>
<div><%= relationship.box.size %></div>
<% end %>
only when I define @relationships in my controller as
@relationships = current_user.relationships
but not when I define it as
@relationships = User.find(current_user.id).relationships
Shouldn’t those two definitions be identical?
I have the following associations:
class Relationship < ActiveRecord::Base
belongs_to :user
belongs_to :box
end
class User < ActiveRecord::Base
has_many: :relationships
has_many: :boxes, through: :relationships
end
class Box < ActiveRecord::Base
has_many: :relationships
has_many: :users, through: :relationships
end
I hope I’ve provided the right information; this is the first time I haven’t been able to find an answer on stack overflow. Thanks in advance!
I had created another object through current_user (current_user.relationships.new) in the same action:
After making the following change to the first assignment, the reload call could be removed:
Special thanks to @AmitKumarGupta for helping a newbie out!