【问题标题】:Rails Tutorial 11.1 - ActiveRecord::HasManyThroughSourceAssociationNotFoundError:Rails 教程 11.1 - ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
【发布时间】:2014-07-02 01:13:18
【问题描述】:

所以我一直在学习 Hartl Rails 教程,但在我们关注其他用户的部分遇到错误。鉴于我正在做教程,你知道我比较笨,但我无法弄清楚问题所在。据我所知,我通过关系适当地使用了 has_many,并且明确地使用了源和外键,但显然缺少一些东西。当它说Is it one of :follower or :followed?

时,我无法解读错误的含义

失败的测试是:

require 'spec_helper'

describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
  end
.
.
.
    describe "following" do
    let(:other_user) {FactoryGirl.create(:user)}
    before do
      @user.save
      @user.follow!(other_user)
    end

    it {should be_following(other_user)}
    ***its(:followed_users) {should include(other_user)}

    describe "followed user" do
      subject {other_user}
      its(:followers) {should include(@user)}
    end

    describe "and unfollowing" do
      before {@user.unfollow!(other_user)}

      it {should_not be_following(other_user)}
      ***its(:followed_users) {should_not include(other_user)}
    end
  end
end

我得到的错误是:

 Failure/Error: its(:followed_users) {should include(other_user)}
 ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
   Could not find the source association(s) :followed_id in model Relationship. Try 'has_many :followed_users, :through => :relationships, :source => <name>'. Is it one of :follower or :followed?

我的模型是:

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation
  has_secure_password
  has_many :microposts, dependent: :destroy
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy
  ***has_many :followed_users, through: :relationships, source: "followed_id"
  has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy

  has_many :followers, through: :reverse_relationships, source: :follower
.
.
.
  def feed
    #this is preliminary 
    Micropost.where("user_id = ?", id)
  end

  def following?(other_user)
    relationships.find_by_followed_id(other_user.id)
  end

  def follow!(other_user)
    relationships.create!(followed_id: other_user.id)
  end

  def unfollow!(other_user)
    relationships.find_by_followed_id(other_user.id).destroy
  end

  private
    def create_remember_token
        self.remember_token = SecureRandom.urlsafe_base64
    end
end

class Relationship < ActiveRecord::Base
  attr_accessible :followed_id

  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "User"

  validates :follower_id, presence: true
  validates :followed_id, presence: true
end

还有我的架构:

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps
    end
    add_index :relationships, :follower_id
    add_index :relationships, :followed_id
    add_index :relationships, [:follower_id, :followed_id], unique: true
  end
end

感谢阅读!如果我遗漏了任何重要的东西,我一定会编辑,但我想我什么都打了。

【问题讨论】:

    标签: ruby-on-rails activerecord has-many-through railstutorial.org


    【解决方案1】:

    你应该使用

    has_many :followed_users, through: :relationships, source: :followed
    

    这是因为source 想要从中加载记录的关联名称(而不是外键)

    【讨论】:

    • Derp 我怎么没注意到相反的关系有正确的表示法...谢谢一堆!
    猜你喜欢
    • 2017-01-13
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    相关资源
    最近更新 更多