Search code examples
ruby-on-railsrubyruby-on-rails-4many-to-many

undefined method `friendships' for #<User::ActiveRecord_Relation:0x007fdd1adb7a00> Rails


I´m learning Rails by building an App. My app was working fine until I added this code to my users_controller.rb

def show
    @user = User.find(params[:id])
  @user = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)

end

def compare
 if current_user.profile    
   @users = User.find_by(id: params[:to].to_i) if params[:to]

    @paper_weight_total_user = @users.papers.sum(:paper_weight) 
  else
    redirect_to user_path
  end
end

I added this to my users_controller because current user is supposed to be able to compare his data to his friends data if he hits the "Compare data" button(shown below)

    <h4> <%= current_user.profile.name%> Friends</h4>
         <ul>
           <% @user.friendships.each do |friendship| %>
           <li>
            <%= link_to user_profile_path(friendship.friend), :method => :get do %>
              <%= friendship.friend.profile.name %>
              (<%= link_to "Remove", friendship, :method => :delete %>)
              <%= link_to friendship do %>

              <%= link_to 'Send message', new_message_path(to: friendship.friend.id), class: 'btn btn-default btn-xs' %> 

              <%= link_to friendship do %>

              <%= link_to 'compare data', compare_friends_path(to: friendship.friend.id), class: 'btn btn-default btn-xs' %>
              <% end %>
          <% end %>
          <% end %>
          <% end %>
          </li>
         </ul> 

The compare_friends_path worked I was able to compare my data to the selected friend data but when I want to direct back to "My page" the app gives me this error:

undefined method `friendships' for #<User::ActiveRecord_Relation:0x007fdd1adb7a00>

And says that this line of code (the do loop) is causing the error:

  <% @user.friendships.each do |friendship| %>

my user.rb model has

has_many :friendships, dependent: :destroy
has_many :friends, through: :friendships

and my friendship.rb model has

belongs_to :user
belongs_to :friend, :class_name => 'User'
belongs_to :profile

I´ve been trying to figure out why this is happening now after I added the compare option but I´m out of luck.... Can anyone here help me out or give me asuggestion to solve this.

Thanks in advance Dadi


Solution

  • undefined method `friendships' for #<User::ActiveRecord_Relation:0x007fdd1adb7a00>
    

    According to this error message, your @user object is a ActiveRecord_Relation object and not an instance of User class, hence you get the error.

    According to your model setup, you can get the friendships for a particular user by using: @user.friendships but this @user has to be an instance of User class but NOT an ActiveRecord_Relation object.

    I think the problem is coming from your show method's this line:

      @user = User.order('created_at DESC').paginate(page: params[:page], per_page: 30)
    

    in this case, @user is a ActiveRecord_Relation object but not a single user. So, when you call @user.friendships in the view, you get the mentioned error.

    You may want to rename this to @users or something like that that makes more sense as this will return a collection of users.

    And, you already have a @user definition in there:

    @user = User.find(params[:id])
    

    where @user is an instance of User class.