ich den Benutzernamen Kommentar Autor ähnlichebelongs_to alias
comment.commenter
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :commenter
belongs_to :commentable, polymorphic: true
end
Modelle erhalten möchten/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :username, presence: true, uniqueness: true
has_many :comments, as: :commenter
end
Wenn ich versuche, Erstellen Sie mithilfe dieser Codezeile einen Kommentar direkt zu db:
Comment.create(commentable_type: 'Pokemon', commentable_id: 1, content: 'great', commenter: 1)
Es wirft diesen Fehler
NameError: uninitialized constant Comment::Commenter
from /var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/inheritance.rb:158:in `compute_type'
ich gelesen habe irgendwo als: verwendet wird nur für polymorphe assocciations so dass der Fall meines Fehlers sein könnte, aber konnte nicht herausfinden, wie dieses Problem zu bekommen
Danke, das mein Problem gelöst –