请求我的示例应用程序的主页时,我收到以下错误信息(以下迈克尔·哈特尔的教程第11章):找不到源协会(S):在Rails的followed_id模型关系3.2
"ActiveRecord::HasManyThroughSourceAssociationNotFoundError in Pages#home"
"Could not find the source association(s) :followed_id in model Relationship. Try 'has_many :followed_users, :through => :relationships, :source => '. Is it one of :follower or :followed?"
这真的很奇怪,因为我完全按照教程的指示进行操作。我甚至复制粘贴每一个代码片段。
我的用户模型(摘录):
class User < ActiveRecord::Base
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
我的关系模型:
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
我一直在试图解决这一问题,但我完全不知道问题可能出在哪里(教程中的确切代码副本)。
您可以将您的解决方案作为答案发布并接受,这样人们就不会花时间阅读问题,只会发现已解决问题。 – EricM
这就是我第一次尝试,但它不会让我说'低于某个声望的用户不能回答自己的问题'。 无论如何,我会把编辑置于问题之上。 对不起,如果你只是读完整个问题才能发现我已经解决了它。 –