Gugubaight Gugubaight - 3 months ago 17
Ruby Question

Rails 4 Error: ActiveRecord::HasManyThroughSourceAssociationNotFoundError




Hey,

this is frustrating. I know what's wrong here, but I have no solution to fix it.

First the error, prompting when clicking on 'Favorite' (/app/views/users/index.html.haml)

ActiveRecord::HasManyThroughSourceAssociationNotFoundError in UsersController#userfavorite

Could not find the source association(s) "userfavorite" or :userfavorites in model FavoriteUser. Try 'has_many :userfavorites, :through => :favorite_users, :source => <name>'. Is it one of c_user_id or user_id?


Request: Parameters:

{"_method"=>"get",
"authenticity_token"=>"VkZF4RtOBSXLFw8mygor24Ty/Efx5uSWxto4qRWf1szu3YwFe1/F5+7QtEXXZv9eaQEQvM5O8ELX95wmPLdZYQ==",
"type"=>"favorite", # What i am doing
"id"=>"1"} # My user id


My goal is to allow Users (from the user model) to favorite other users (user model) through the favoriteuser model. The conflict here: I am not able to do the associations right cause I have only one model the data is coming from.

Let me show you the code real quick! (Conflicts user.rb)

app/models/favorite_user.rb

class FavoriteUser < ActiveRecord::Base
belongs_to :c_user_id
belongs_to :user_id
end


app/models/user.rb

class User < ActiveRecord::Base

# Include default devise modules. Others available are:

# :confirmable, :lockable, :timeoutable and :omniauthable

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


has_many :tools

# Favorite tools of user
has_many :favorite_tools # just the 'relationships'
has_many :favorites, through: :favorite_tools, source: :tool # the actual tools the user favorites

# Favorite users of user
has_many :favorite_users # just the 'relationships'
has_many :userfavorites, through: :favorite_users, source: :user # conflicting! Can't link to userscontroller cause it belongs to the user model, the model we are at right now.
has_many :userfavorited_by, through: :favourite_users, source: :user # conflicting! Can't link to userscontroller cause it belongs to the user model, the model we are at right now.



mount_uploader :avatar_filename, AvatarUploader

end


app/controllers/users_controller.rb

class UsersController < ApplicationController
before_action :find_user, only: [:show, :favorite]

# Add and remove favorite recipes
# for current_user
def userfavorite
type = params[:type]
if type == "favorite"
current_user.userfavorites << @user
redirect_to :back

elsif type == "unfavorite"
current_user.userfavorites.delete(@user)
redirect_to :back
else
# Type missing, nothing happens
redirect_to :back, notice: 'Nothing happened.'
end
end

def index
@users = User.all
end

def show
@tools = Tool.where(user_id: @user).order("created_at DESC")
@tool = Tool.find(1)
end

private

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


app/views/users/index.html.haml

- @users.each do |user|
= image_tag gravatar_for user if user.use_gravatar == true
= image_tag user.avatar_filename.url if user.use_gravatar == false
%h2= link_to user.username, user
%p= link_to "Favorite", favorite_user_path(user, type: "favorite"), method: :get
%p= link_to "Unfavorite", favorite_user_path(user, type: "unfavorite"), method: :get


app/config/routes.rb

resources :users, only: [:index, :show, :userfavorite] do
get :userfavorite, on: :member
end


Attributes of :favorite_users

c_user_id:integer user_id:integer


(=> c_user_id for currents user id - so the user who is adding favorite users)




I hope the provided data is enough, if not please tell me. I'm grateful for all Your replies.

Answer

Here is my suggestion:

user.rb

userfavorites - will list all user you have favorited

has_many :favorite_relationships, class_name: "FavoriteUser", foreign_key: "c_user_id"
has_many :userfavorites, through: :favorite_relationships, source: :user

userfavorited_by - will list all user who favorited you

has_many :favorited_relationships, class_name: "FavoriteUser", foreign_key: "user_id"
has_many :userfavorited_by, through: :favorited_relationships, source: :c_user

FavoriteUser

edited:

class FavoriteUser < ActiveRecord::Base
    belongs_to :c_user, class_name: "User"
    belongs_to :user, class_name: "User"
end

After you make changes, make sure to test first in your console that the relationships works. then make appropriate change in your view, controller etc...