我正在学习rails,当我运行rake db:seed时注意到了这个问题。老实说,我不确定问题是什么,因为一切似乎运行良好,所有的关联似乎也在起作用。
错误讯息:
<string><space><string><tab><number><tab><number><tab>...
这是我种子的一个例子:
rake aborted! ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "author" or :author in model Post. Try 'has_many :author, :through => :post, :source => <name>'. Is it one of user, category, comments, comment_users, likes, or like_users?
/home/houtai/hr.glass/ourz/db/seeds.rb:102:in `<top (required)>'
Tasks: TOP => db:seed
以下是我的协会:
User.create!({
first_name: 'Mark',
last_name: 'Wong',
bio: 'bla',
email: 'mark@gmail.com',
password: 'test',
password_confirmation: 'test',
profile_pic: 'http://houtaiwong.com/img/profile.jpg'
})
User.create!({
first_name: 'Hou',
last_name: 'Wong',
bio: 'bla',
email: 'test@gmail.com',
password: 'test',
password_confirmation: 'test',
profile_pic: 'https://pmcdeadline2.files.wordpress.com/2016/02/alicia-vikander-the-danish-girl.jpg'
})
Category.create!({
name: 'Music'
})
Category.create!({
name: 'Blog'
})
Category.create!({
name: 'Video'
})
Category.create!({
name: 'Picture'
})
Post.create!({
title: 'Music',
content: 'text',
author: User.find_by(first_name: 'Mark').id,
category_id: Category.find_by(name: 'Music').id,
end_time: '12/1/2016',
image: 'https://pmcdeadline2.files.wordpress.com/2016/02/alicia-vikander-the-danish-girl.jpg'
})
发表:
class User < ActiveRecord::Base
authenticates_with_sorcery!
# attr_accessor :remote_image_url, :first_name, :last_name, :bio, :emaily, :profile_pic
validates :password, length: { minimum: 3 }, if: -> { new_record? || changes[:crypted_password] }
validates :password, confirmation: true, if: -> { new_record? || changes[:crypted_password] }
validates :password_confirmation, presence: true, if: -> { new_record? || changes[:crypted_password] }
validates :email, uniqueness: true
has_many :posts, foreign_key: :author
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
has_many :comments
has_many :comment_posts, through: :comments, source: 'post'
has_many :likes
has_many :like_posts, through: :likes, source: 'post'
答案 0 :(得分:1)
在Post模型的关系中,你有:
belongs_to :user, foreign_key: :author
这意味着rails关系是用户而非作者,但因为您将foreign_key设置为author,当ActiveRecord在数据库中查找时,它将使用author_id字段。
所以在你的种子里:
Post.create!({
title: 'Music',
content: 'text',
author: User.find_by(first_name: 'Mark').id,
category_id: Category.find_by(name: 'Music').id,
end_time: '12/1/2016',
image: 'https://pmcdeadline2.files.wordpress.com/2016/02/alicia-vikander-the-danish-girl.jpg'
})
作者密钥将无法识别,因为您已将该关系声明为用户。
两种可能的解决方案是将种子改为:
Post.create!({
title: 'Music',
content: 'text',
user: User.find_by(first_name: 'Mark').id,
category_id: Category.find_by(name: 'Music').id,
end_time: '12/1/2016',
image: 'https://pmcdeadline2.files.wordpress.com/2016/02/alicia-vikander-the-danish-girl.jpg'
})
或将您在Post中的关系更改为:
belongs_to :author, class_name: 'User'
这应解决Could not find the source association(s) "author" or :author in model Post
部分,但是你有很多关系,并且通过表格进行,可能需要更多信息才能全面了解。