标签: after-save

如何在 ruby​​ on rails 中计算特定的评级计数哈希?

所以,我在评论模型上有一个after_save钩子,它调用了产品模型的calculate_specific_rating函数。函数是这样的:

def calculate_specific_rating
    ratings = reviews.reload.all.pluck(:rating)
    specific_rating = Hash.new(0)
    ratings.each { |rating| specific_rating[rating] += 1 }
    self.specific_rating = specific_rating
    save
  end
Run Code Online (Sandbox Code Playgroud)

现在,它返回

specific_rating => { 
"2"=> 3, "4"=> 1
}
Run Code Online (Sandbox Code Playgroud)

我希望它返回如下:

specific_rating => { 
"1"=> 0, "2"=>3, "3"=>0, "4"=>1, "5"=>0
}
Run Code Online (Sandbox Code Playgroud)

另外,每次保存评论时都可以初始化一个新的哈希吗?我想要一些替代品。谢谢

ruby hash ruby-on-rails after-save

7
推荐指数
1
解决办法
315
查看次数

Rails:检查模型是否真的保存在after_save中

ActiveRecord用于在每次调用save方法时调用after_save回调,即使模型未更改且未生成插入/更新查询.

这实际上是默认行为.在大多数情况下,这是可以的.

但是一些after_save回调对于实际保存模型的事情是敏感的.

有没有办法确定模型是否实际保存在after_save中?

我正在运行以下测试代码:

class Stage < ActiveRecord::Base
  after_save do
    pp changes
  end
end

s = Stage.first
s.name = "q1"
s.save!
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails callback after-save

5
推荐指数
2
解决办法
1万
查看次数

rspec测试has_many:through和after_save

我有一个(我认为)has_many :through与连接表相对简单的关系:

class User < ActiveRecord::Base
  has_many :user_following_thing_relationships
  has_many :things, :through => :user_following_thing_relationships
end

class Thing < ActiveRecord::Base
  has_many :user_following_thing_relationships
  has_many :followers, :through => :user_following_thing_relationships, :source => :user
end

class UserFollowingThingRelationship < ActiveRecord::Base
  belongs_to :thing
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

这些rspec测试(我知道这些不一定是好的测试,这些只是为了说明发生了什么):

describe Thing do     
  before(:each) do
    @user = User.create!(:name => "Fred")
    @thing = Thing.create!(:name => "Foo")    
    @user.things << @thing
  end

  it "should have created a relationship" do
    UserFollowingThingRelationship.first.user.should == @user
    UserFollowingThingRelationship.first.thing.should == @thing
  end

  it "should have followers" do
    @thing.followers.should …
Run Code Online (Sandbox Code Playgroud)

ruby rspec ruby-on-rails after-save has-many-through

5
推荐指数
1
解决办法
6306
查看次数

如何知道在 Parse Cloud Code afterSave 钩子中的值是否改变?

每当我的对象的单个键的值在解析云代码afterSave挂钩中发生变化时,我都想发送推送通知。

Parse.Cloud.afterSave("Channel", function(request) {  
    var channel = request.object
    // TODO: check if value of channel key "state" was changed
});
Run Code Online (Sandbox Code Playgroud)

如何检查密钥的值是否state已更新?

这是我可以从request对象中获得的所有数据:http : //parseplatform.org/Parse-SDK-JS/api/v1.11.0/Parse.Cloud.html#.TriggerRequest

该线程中建议的解决方案感觉不对:Parse Javascript API Cloud Code afterSave with access to beforeSave values

我知道我可以通过beforeSave钩子中的脏方法来做到这一点。但是,这对我不起作用。为什么?如果我确实向许多用户发送推送通知,这需要一些时间。接收推送通知的客户端开始从服务器请求更新的频道对象。然而,他们可能会收到旧版本的对象,因为只要beforeSave尚未完成发送所有推送,通道对象就不会持久保存在数据库中。

javascript after-save before-save parse-cloud-code parse-server

4
推荐指数
1
解决办法
1155
查看次数

Rails ActiveRecord使用after_save回调中最近保存的记录ID

我想在after_save回调中使用最近存储的记录ID,如果可以的话,这可能吗?

after_save :create_children

def create_children
 self.id
end

更新:抱歉,我的保存功能出了点问题,不好,很抱歉浪费您的时间

谢谢

ruby-on-rails callback after-save

2
推荐指数
1
解决办法
5296
查看次数

在rails 3中弃用了after_save解决方案

嗨我在rails 3中创建一个应用程序我正在使用paperclip,其中我想调用一个回形针函数来生成图像的缩略图但是当我调用after_save方法时它会给出错误

弃用警告:不推荐使用Base#after_save,请改用Base.after_save:方法.(来自app/models/asset.rb:23)

ruby-on-rails paperclip after-save ruby-on-rails-3

0
推荐指数
1
解决办法
2915
查看次数

Rails after_save错误

这是使用after_save回调的正确方法吗?

class CouponsController < ApplicationController
after_save :remove_restrictions
 private
    def remove_restrictions

      logger.debug("in after save")
    end

end
Run Code Online (Sandbox Code Playgroud)

此代码将错误抛出为

undefined method `after_save' for CouponsController:Class
Run Code Online (Sandbox Code Playgroud)

使用after_save的正确方法是什么?

callback after-save ruby-on-rails-3

0
推荐指数
1
解决办法
1633
查看次数