Ask A Question

Notifications

You’re not receiving notifications from this thread.

Comments With Polymorphic Associations Discussion

Jessica Ellias Jessica Ellias
Reply

Thank you Chris!

Reply

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.

Reply

That's great to hear! :D And I agree, it's a tough one to wrap your head around the first time.

Reply
Khemlall Mangal Khemlall Mangal

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="">

Reply

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.

Reply
Dinesh Pallapa Dinesh Pallapa

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.

Reply

@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

Reply

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

Reply
Being new at programming and Rails this is the first time I land on the polymorphic association concept and got It completely. I find It super useful and You made It "easy" to understand and implement. Thanks!
Reply

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

Reply

It would great if you could expand this to include comment replies. There are no great tutorials on how to accomplish this task.

Reply

Been planning on doing that soon. Thinking about doing this in a series where we create an embeddable Javascript comment system like Disqus.

Reply

Do it!

Reply

I'd love to see this as well. Tried briefly and was unsuccessful.

Reply

That would be awesome. Thanks!

Reply

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 ?

Reply

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

Reply

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.

Reply

@post.comments.users would work if you had your comments has_many: users association on your comment model.

Reply

I have belongs_to :user in my Comment model

Reply

belongs_to :commentable, polymorphic: true
belongs_to :user

Reply

Got it! In my comments.rb:

has_many :commenters, through: :comments, source: :user

Reply

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.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.