Rails教程11.1 - ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:

时间:2014-07-02 01:13:18

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

所以我一直在通过Hartl Rails教程工作,我遇到了一个错误,我们跟随其他用户。鉴于我正在做教程,你知道我是相对noobish,但我无法弄清楚问题。据我所知,正确使用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

感谢阅读!如果我遗漏任何重要的东西,我一定会编辑,但我想我已经完成了所有事情。

1 个答案:

答案 0 :(得分:0)

你应该使用

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

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