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


i´m learning rails building app. app working fine until added code 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 users_controller because current user supposed able compare data friends data if hits "compare data" button(shown below)

    <h4> <%= current_user.profile.name%> friends</h4>          <ul>            <% @user.friendships.each |friendship| %>            <li>             <%= link_to user_profile_path(friendship.friend), :method => :get %>               <%= friendship.friend.profile.name %>               (<%= link_to "remove", friendship, :method => :delete %>)               <%= link_to friendship %>                <%= link_to 'send message', new_message_path(to: friendship.friend.id), class: 'btn btn-default btn-xs' %>                 <%= link_to friendship %>                <%= 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 able compare data selected friend data when want direct "my page" app gives me error:

undefined method `friendships' #<user::activerecord_relation:0x007fdd1adb7a00> 

and says line of code (the loop) causing error:

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

my user.rb model has

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

and friendship.rb model has

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

i´ve been trying figure out why happening after added compare option i´m out of luck.... can here me out or give me asuggestion solve this.

thanks in advance dadi

undefined method `friendships' #<user::activerecord_relation:0x007fdd1adb7a00> 

according error message, @user object activerecord_relation object , not instance of user class, hence error.

according model setup, can friendships particular user using: @user.friendships @user has instance of user class not activerecord_relation object.

i think problem coming show method's line:

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

in case, @user activerecord_relation object not single user. so, when call @user.friendships in view, mentioned error.

you may want rename @users or that makes more sense return collection of users.

and, have @user definition in there:

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

where @user instance of user class.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -