这是我的like.rb 模型
class Like < ActiveRecord::Base
attr_accessible :user_id, :post_id
before_save :increment_post_total_likes
before_destroy :decrement_post_total_likes
scope :desc_ordered, :order => "created_at DESC"
belongs_to :post
belongs_to :user
def increment_post_total_likes
p = Post.find(self.post_id)
p.increment!(:total_likes)
p.save
end
def decrement_post_total_likes
p = Post.find(self.post_id)
p.decrement!(:total_likes)
p.save
end
def self.alreadyLiked(post_id, user_id)
where(:user_id => user_id, :post_id => post_id).any?
end
def self.already_liked?(user_id)
exists?(:user_id => user_id)
end
end
问题是,当我喜欢一个帖子时,它会增加帖子total_likes
列,但也会增加另一个表列,这两个列共享相关的列post_id
,user_id
我不明白为什么......
我只想在likes表上使用 before_save 和 before_destroy