【问题标题】:Rails 4 Error: ActiveRecord::HasManyThroughSourceAssociationNotFoundErrorRails 4 错误:ActiveRecord::HasManyThroughSourceAssociationNotFoundError
【发布时间】:2017-01-13 02:47:47
【问题描述】:


嘿,
这令人沮丧。我知道这里出了什么问题,但我没有解决办法。

首先报错,点击“收藏”时提示(/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?

请求:参数:

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

我的目标是让用户(来自用户模型)通过喜爱用户模型来收藏其他用户(用户模型)。这里的冲突:我无法正确进行关联,因为我只有一个数据来自的模型。

让我快速向您展示代码! (冲突用户.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

:favorite_users 的属性

c_user_id:integer user_id:integer

(=> c_user_id for currents user id - 所以添加喜欢的用户的用户)


我希望提供的数据足够,如果没有请告诉我。感谢您的所有回复。

【问题讨论】:

  • :favorite_users 模型的属性是什么?
  • @mrvncaragay c_user_id:integer user_id:integer (=> c_user_id for currents user id - 所以添加喜爱用户的用户)
  • 你没有正确建立你的关系,例如foreign_key。生病写我的建议

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord associations


【解决方案1】:

这是我的建议:

user.rb

userfavorites - 将列出您收藏的所有用户

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

userfavorited_by - 将列出所有收藏你的用户

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

收藏用户

已编辑:

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

进行更改后,请确保 test 首先在您的 console 中建立关系。然后在您的视图、控制器等中进行适当的更改...

【讨论】:

  • 感谢您的回答,听起来不错。但是,当我尝试 chrome 输出一些非常奇怪的东西时,因为它在 FavoriteUser 类 c:/Users/Jonas/gitapps/ocubit/app/models/favorite_user.rb:2: 语法错误,意外 tSTRING_BEG 中缺少“do”,期待 keyword_do 或 '{' 或 '(' belongs_to :c_user_id, class_name "User" ^ c:/Users/Jonas/gitapps/ocubit/app/models/favorite_user.rb:3: 语法错误,意外 tSTRING_BEG,期待 keyword_do 或 ' {' or '(' belongs_to :user_id, class_name "用户" ^
  • 不是“做”的事情,而是一些其他的东西,通常不在课堂上
  • 是的,这很奇怪。我正在查看我发布的代码,但没有看到任何语法错误。您是否在控制台中对其进行了测试?
  • 这就是它在控制台中的作用(同样的错误): irb(main):002:0> User.find(1).userfavorites
  • SyntaxError: c:/Users/Jonas/gitapps/ocubit/app/models/favorite_user.rb:2: 语法错误,意外的 tSYMBEG,期待 keyword_do 或 '{' 或 '(' {belongs_to : c_user_id, class_name "User"} ^ c:/Users/Jonas/gitapps/ocubit/app/models/favorite_user.rb:2: 语法错误,意外'}',期待keyword_end c:/Users/Jonas/gitapps/ocubit/ app/models/favorite_user.rb:3: 语法错误,意外'}',期待来自的keyword_end
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
相关资源
最近更新 更多