ruby-on-rails - 为什么有时这仅在特定记录上不起作用?

标签 ruby-on-rails ruby-on-rails-3

我进行了一项交易,计算 current_user 的社区拥有多少成员,以及每个社区拥有多少成员。

如果其他注册用户在特定社区按“加入”。
用户将成为社区成员,社区所有者获得1积分。

当然,当用户从社区中按“退出”时,它会执行相反的操作。

我是这样编码的,到目前为止工作正常。
不过,我发现只有一个社区不接受这一交易。
事实上,它会切换标志,但没有这些计算和计数事务。 所以积分数量和成员(member)数量不会改变:( 看起来记录几乎被锁定了!

为什么会发生这种情况?只有一条记录!!

Controller

    if current_user.voted_up_on? @community
        current_user.dislikes @community
        
        #Calculate the points of the community's owner
        if @community.user
            @user = User.find(@community.user_id)
            @user.profile.total_point = Community.sum(:cached_votes_up, :conditions => ["user_id = ?", @user]) + @user.profile.bonus_point
            @user.profile.next_level = next_level_set(@user)
            @user.save
        end
        
        #count how many members the community has.
        @users =  User.where(:id => @community.likes.map(&:voter_id))
        @community.cached_votes_up = @users.count           
        @community.save
        
        
    else
        current_user.likes @community
        
        #Calculate the points of the community's owner
        if @community.user
            @user = User.find(@community.user_id)
            @user.profile.total_point = Community.sum(:cached_votes_up, :conditions => ["user_id = ?", @user]) + @user.profile.bonus_point
            @user.profile.next_level = next_level_set(@user)
            @user.save
        end
        
        #count how many members the community has.
        @users =  User.where(:id => @community.likes.map(&:voter_id))
        @community.cached_votes_up = @users.count           
        @community.save
                    
        
    end

最佳答案

我猜测您正在尝试在 @community 上调用 save,但由于某种原因(可能是验证错误)未能保存。我建议检查 @community.save 的结果,如果是 false,请检查 @community.errors 是否有验证错误。另请参阅Rails Guide就这个主题而言。

关于ruby-on-rails - 为什么有时这仅在特定记录上不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15515840/

相关文章:

javascript - 遍历数组 javascript google map 标记

ruby-on-rails - 从对 super 的调用中清理一个 block

ruby-on-rails - MySQL2 gem 无法安装

ruby-on-rails - 未知验证器 : 'BeforeValidator' when validating date in Rails

ruby-on-rails - Rails 3 不推荐使用的方法和 API

ruby-on-rails-3 - extjs 4拖放2个网格之间没有插件?

ruby-on-rails - RAILS : Does ActiveRecord#persisted? 实际查询数据库?

ruby-on-rails - 友好的 ID 和 Rails 引擎

ruby-on-rails - 没有要加载的文件 -- nokogiri

ruby-on-rails - Heroku 迁移 : type modifier is not allowed for type "bytea"