2017-02-02 20 views
0

Ich lerne gerade Schienen und bemerkte dieses Problem, wenn ich Rake db: Seed lief. Ich bin ehrlich gesagt nicht sicher, was das Problem ist, weil alles scheint gut zu laufen und alle Assoziationen scheinen auch zu arbeiten.Rails Active Record hat viele Association Fehler

Fehlermeldung:

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 

Hier ist ein Beispiel für meine Samen:

User.create!({ 
    first_name: 'Mark', 
    last_name: 'Wong', 
    bio: 'bla', 
    email: '[email protected]', 
    password: 'test', 
    password_confirmation: 'test', 
    profile_pic: 'http://houtaiwong.com/img/profile.jpg' 
    }) 

User.create!({ 
    first_name: 'Hou', 
    last_name: 'Wong', 
    bio: 'bla', 
    email: '[email protected]', 
    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' 
}) 

Hier sind meine Assoziationen:

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' 

Beitrag:

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 
+0

Aber die Fehlermeldung hat nichts mit den Kommentaren zu tun. –

Antwort

1

In Ihrer Beziehung in Post-Modell Sie haben:

belongs_to :user, foreign_key: :author 

Dies bedeutet, dass die Schienen Relation Benutzer nicht Autor, sondern weil Sie die foreign_key müssen Autor gesetzt, wenn Active in der Datenbank sucht sie verwenden die Feld Autor_ID

Also in Ihrem Samen, wo Sie haben:

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' 
}) 

der Autor Schlüssel werden nicht erkannt, da Sie die Beziehung als Benutzer angegeben haben.

Zwei mögliche Lösungen sind Ihre Samen ändern:

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' 
}) 

oder Ihre Beziehung in Post ändern:

belongs_to :author, class_name: 'User' 

Dies sollte die Could not find the source association(s) "author" or :author in model Post Teil lösen aber Sie viele Beziehungen haben und durch Tische, die dort gehen und mehr Informationen können erforderlich sein, um das ganze Bild zu erhalten.