2
我有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
正如你可以看到我有同樣的回調。它如何更容易?如果我用回調製作模塊,這是正確的嗎?
確定。謝謝。我將創建該模塊。 – Mike 2012-03-11 11:41:32