Problem in mutiple :dependent=> :destroy when multiple polymorphic is true
Posted
by piemesons
on Stack Overflow
See other posts from Stack Overflow
or by piemesons
Published on 2010-06-16T10:40:07Z
Indexed on
2010/06/16
10:42 UTC
Read the original article
Hit count: 213
I have four models question, answer, comment and vote.Consider it same as stackoverflow.
- Question has_many comments
- Answers has_many comments
- Questions has_many votes
- answers has_many votes
- comments has_many votes
Here are the models (only relevant things)
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
has_many :votes, :as => :votable, :dependent => :destroy
end
class Question < ActiveRecord::Base
has_many :comments, :as => :commentable, :dependent => :destroy
has_many :answers, :dependent => :destroy
has_many :votes, :as => :votable, :dependent => :destroy
end
class Vote < ActiveRecord::Base
belongs_to :votable, :polymorphic => true
end
class Answer < ActiveRecord::Base
belongs_to :question, :counter_cache => true
has_many :comments, :as => :commentable , :dependent => :destroy
end
Now the problem is whenever i am trying to delete any question/answer/comment its giving me an error
NoMethodError in QuestionsController#destroy
undefined method `each' for 0:Fixnum
if i remove this line from any of the model (question/answer/comment)
has_many :votes, :as => :votable, :dependent => :destroy
then it works perfectly. It seems there is a problem while deleting the records active record is not able to find out the proper path because of multiple joins within the tables.
© Stack Overflow or respective owner