class PraiseTreadController < ApplicationController def praise_plus @obj = nil if request.get? @obj = params[:obj] # 传的是对象,最后变成id了 #首先创建或更新praise_tread 表 @pt = PraiseTread.find_by_user_id_and_praise_tread_object_id(User.current.id,@obj) @pt = @pt.nil? ? PraiseTread.new : @pt @pt.user_id = User.current.id @pt.praise_tread_object_id = @obj.to_i @pt.praise_tread_object_type = User.find_by_id(@obj).class.name.underscore @pt.praise_or_tread = 1 @pt.save #再创建或更新praise_tread_cache表 @ptc = PraiseTreadCache.find_by_object_id(@obj) @ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc @ptc.object_id = @obj.to_i @ptc.object_type = User.find_by_id(@obj).class.name.underscore @ptc.plus(1) @ptc.save end @obj = User.find_by_id(@obj) respond_to do |format| format.html format.js end end def praise_minus @obj = nil if request.get? @obj = params[:obj] # 传的是对象,最后变成id了 #首先更新praise_tread 表 删除关注记录 @pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user") @pt.delete #再更新praise_tread_cache表 使相应的记录减1 当为0时删除 @ptc = PraiseTreadCache.find_by_object_id(@obj) @ptc.minus(1) if @ptc.praise_num == 0 @ptc.delete end end @obj = User.find_by_id(@obj) respond_to do |format| format.html format.js end end def tread_plus end def tread_minus respond_to do |format| format.html format.js end end end