Comments With Polymorphic Associations Discussion
Just coming back here to say thanks! I watched this several times and it eventually sunk in. I managed to set it up a few months ago and it's been working quite well. I will admit it did take me some time to grasp the concept though.
That's great to hear! :D And I agree, it's a tough one to wrap your head around the first time.
Hi all, i am getting issue with my routes when i follow this methodology. I get uninitialized constant Squeals.
SQUEAL Models
class Squeal < ActiveRecord::Base
has_many :comments, as: :commentable
end
comment.rb
class Comment < ActiveRecord::Base
belongs_to:commentable, polymorphic:true
end
/squeal/comments_controller.rb
class Squeals::CommentsController <commentscontroller before_action="" :set_commentable="" private="" def="" set_commentable="" @commentable="Squeal.find(params[:squeal_id])" end="" end="" comments="" controller="" class="" commentscontroller="" <="" applicationcontroller="" before_action:authenticate_user!="" def="" create="" @comment="@commentable.comments.new" comment_params="" @user.user="current_user" comment.save="" redirect_to="" @commentable,="" notice:="" "your="" comment="" was="" posted"="" end="" private="" def="" comment_params="" params.require(:comment).permit(:body)="" end="" end="" routes="" resources="" :squeals="" do="" resources="" :comments,="" module:="" :squeals="" end="">
Hi Chris, I want to add comments to multiple films at the same time. Suppose I want to add comments section on index page of films. Can you please help me how can i do that.
thanks,It's really help full.If we want to delete the comment from actor or film we need to define a method as destroy or else using _destroy for nested attributes.Can anyone help me out.
@excid3:disqus
How would you need to modify this to work with deeply nested resources?
i.e:
resources :projects do
resources :project_users, path: :users, module: :projects
resources :posts do
resources :comments, module: :posts
end
end
I am trying to use commentable with actioncable , but I keep getting the following error
d6f951d17) from Async(default) in 20.73ms: ActionView::Template::Error (undefined method `comments' for #<class:0x007f5cec08b880>):
someone advice me to look for value of comments and I realized is not define
but my question is Is it necessary to define relationship between comment and lets say article class ?
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true #, optional: true
belongs_to :article, optional: true <--- this point
validates :body, presence: true, length: {minimum: 5, maximimum: 1000 }
after_create_commit {CommentBroadcastJob.perform_later self}
end
Hi Chris! Can this form be used on an index page? I'm noticing that I only get it to work on a show page. Any help
It would great if you could expand this to include comment replies. There are no great tutorials on how to accomplish this task.
Been planning on doing that soon. Thinking about doing this in a series where we create an embeddable Javascript comment system like Disqus.
How we delete these polymorphic comments while we can't type the actor_comment_path or film_comment_path ? i used case
statement, any smarter idea ?
How can I get all of the users that have commented? What I would like is to be able to:
@post.commenters.each do |u|
something here...
end
To make it as commenters
you'd need to set an alias on the users association on comments. Hope this helps. Sorry I don't have exact code for it.
@post.comments.users
would work if you had your comments has_many: users
association on your comment
model.
If you use:
rails g model Comment commentable:references{polymorphic}
rails auto creates the compound index for lookup as part of the migration:
t.references :commentable, polymorphic: true
This works in Rails 5 and probably goes back to rails 4.