comment_threads and root_comments do not work with model inheritance
Closed this issue · 2 comments
stanguy commented
If my commentable model has inherited models, using comment_threads or root_comments does not work as expected.
Steps to reproduce:
% rails new aacwt [ cd aacwt, edit Gemfile, run bundle ] % rails generate acts_as_commentable_with_threading_migration % rails g model Post % rails g model Article --parent=Post % rails g model User % rake db:migrate
> a = Article.create > u = User.create > c = Comment.build_from( a, u, "hello, world" ) > c.save > a.root_comments => [] # What!?
sqlite> select * from comments ; 1|1|Article||hello, world||1||1|2|2010-11-17 12:27:40.157701|2010-11-17 12:27:40.157701
Here, we have an Article as commentable_type.
% tail -1 log/development.log Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE ("comments".commentable_id = 1 AND "comments".commentable_type = 'Post') AND ("comments"."parent_id" IS NULL) ORDER BY created_at ASC
But it looks for a Post commentable_type.
ChristianPeters commented
Well... I guess that's more or less a Ruby / Rails issue. But maybe elight can provide a workaround?
We did the following...
@commentable = ConfigurableProduct.find(params[:commentable_id])
# A dummy with the STI base class of the commentable is necessary
# as the base class is expected when reading from the comments table
dummy_commentable = @commentable.class.base_class.new
dummy_commentable.id = @commentable.id
@comment = Comment.build_from(dummy_commentable,
current_user.id,
params[:comment][:body])
There is also a Rails method becomes
which basically does the same: casting an object to the base class by creating a duplicate. However, we had huge problems with that because that screwed up all Product instances in the app when posting a comment (and executing becomes
on one ConfigurableProduct).