2
Mongoid 3에서 다형성 모델의 부모에 액세스하는 방법이 있습니까? 나는이 관계를has_many 관계의 부모 액세스
class Project
...
field "comments_count", :type => Integer, :default => 0
has_many :comments, :as => :commentable
...
end
class Comment
...
field "status"
belongs_to :commentable, :polymorphic => true
before_validation :init_status, :on => :create
after_create :increase_count
def inactivate
self.status = "inactive"
decrease_count
end
private
def init_status
self.status = 'active'
end
def increase_count()
@commentable.inc(:comments_count, 1)
end
def decrease_count()
@commentable.inc(:comments_count, -1)
end
...
end
댓글이 아이에 count()
을하고 있기 때문에 비활성화 될 때 나는 부모 관계에있는 comments_count
를 업데이트 할 수 있도록하고 싶습니다 (그리고 내가해야 할 것입니다 매우 비싼 그 응용 프로그램에서 많이). increase_count
이 작동하지만 @commentable
에 decrease_count
(@commentable = nil
)을 액세스 할 수 없습니다. 어떤 아이디어?
깔끔하고 작동합니다. – user2398029