[英]Rails Active Record Has Many Association Errors
我現在正在學習Rails,並且在運行rake db:seed時注意到了這個問題。 老實說,我不確定問題是什么,因為一切似乎都運行良好,並且所有關聯似乎也都正常運行。
錯誤信息:
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'
發布:
class Post < ActiveRecord::Base
require 'time'
belongs_to :user, foreign_key: :author
belongs_to :category
has_many :comments
has_many :comment_users, through: :comments, source: 'user'
has_many :likes
has_many :like_users, through: :likes, source: 'user'
validates :title, presence: true
在Post模型的關系中,您具有:
belongs_to :user, foreign_key: :author
這意味着rails關系不是用戶,不是author,但是由於您將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
部分中Could not find the source association(s) "author" or :author in model Post
但是您有很多關系,並且要通過那里的表格進行操作,可能需要更多信息才能獲得完整圖片。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.