class PraiseTread < ActiveRecord::Base attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread belongs_to :user belongs_to :praise_tread_object, polymorphic: true after_create :be_user_score def self.find_object_by_type_and_id(type,id) @obj = nil case type when 'User' @obj = User.find_by_id(id) when 'Issue' @obj = Issue.find_by_id(id) when 'Project' @obj = Project.find_by_id(id) when 'Bid' @obj = Bid.find_by_id(id) when 'Contest' @obj = Contest.find_by_id(id) when 'Memo' @obj = Memo.find_by_id(id) when 'Message' @obj = Message.find_by_id(id) end return @obj end #更新用户分数 - by zjc def be_user_score #踩贴吧或讨论区帖子 if self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Memo' || self.praise_tread_object_type == 'Message') obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) target_user = obj.author UserScore.skill(:treaded_by_user, User.current,target_user,self, { praise_tread_id: self.id }) #顶贴吧或讨论区帖子 elsif self.praise_or_tread == 1 && (self.praise_tread_object_type == 'Memo' || self.praise_tread_object_type == 'Message') obj = PraiseTread.find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id) target_user = obj.author UserScore.skill(:praised_by_user, User.current,target_user,self,{ praise_tread_id: self.id }) #更新用户等级 UserLevels.update_user_level(target_user) end end end