我有 3 个模型和多态关系。邮政:
#models/post.rb
class Post < ActiveRecord::Base
after_create :create_vote
has_one :vote, :dependent => :destroy, :as => :votable
protected
def create_vote
self.vote = Vote.create(:score => 0)
end
end
评论:
#models/comment.rb
class Comment < ActiveRecord::Base
after_create :create_vote
has_one :vote, :dependent => :destroy, :as => :votable
protected
def create_vote
self.vote = Vote.create(:score => 0)
end
end
投票(多态)
#models/vote.rb
class Vote < ActiveRecord::Base
belongs_to :votable, :polymorphic => true
end
如您所见,我有相同的回调。怎么更容易?如果我制作一个带有回调的模块,这对吗?