Ruby on rails Rails教程11.1-ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
因此,我一直在努力学习HartlRails教程,在我们关注其他用户的部分,我遇到了一个错误。考虑到我在做这个教程,你知道我相对来说是个呆子,但我无法解决这个问题。据我所知,我正确地使用了has_many through关系,并且明确地使用了源代码和外键,但是我显然遗漏了一些东西。我无法理解错误是什么意思,当它说Ruby on rails Rails教程11.1-ActiveRecord::HasManyThroughSourceAssociationNotFoundError:,ruby-on-rails,activerecord,has-many-through,railstutorial.org,Ruby On Rails,Activerecord,Has Many Through,Railstutorial.org,因此,我一直在努力学习HartlRails教程,在我们关注其他用户的部分,我遇到了一个错误。考虑到我在做这个教程,你知道我相对来说是个呆子,但我无法解决这个问题。据我所知,我正确地使用了has_many through关系,并且明确地使用了源代码和外键,但是我显然遗漏了一些东西。我无法理解错误是什么意思,当它说它是一个:follower还是:followerd? 未能通过的测试包括: require 'spec_helper' describe User do before do
它是一个:follower还是:followerd?
未能通过的测试包括:
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?
失败/错误:其(:跟随的用户){应包括(其他用户)}
ActiveRecord::HasManyThroughSourceAssociationNotFound错误:
找不到源关联:在模型关系中跟随\u id。请尝试“has_many:following_users,:through=>:relationships,:source=>”。是跟随者还是跟随者?
我的模型是:
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用户
和我的模式:
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
class CreateRelationships
谢谢你的阅读!如果我遗漏了任何重要的内容,我一定会编辑,但我想我会找到所有内容。您应该使用
has_many :followed_users, through: :relationships, source: :followed
这是因为
source
需要从中加载记录的关联的名称(不是外键)Derp我怎么没有注意到相反的关系有正确的表示法。。。非常感谢!