标签: beforeupdate

Rails:如何只为一个更改的属性运行before_update?

在我的模型中,Shop我正在保存图像URL logo_ori并使用它来制作缩略图before_update.

# shop.rb
before_update :run_blitline_job

private

def run_blitline_job
  # uses logo_ori to make thumbnails
end
Run Code Online (Sandbox Code Playgroud)

但是我发现当我保存其他属性时(例如:在表单中编辑商店的个人资料),它也会运行before_update.仅logo_ori在保存时如何限制其执行?

我试过这个:

before_update :run_blitline_job, :if => :logo_ori?
Run Code Online (Sandbox Code Playgroud)

before_update如果我logo_ori早先已经保存过,它仍会运行.

activerecord ruby-on-rails beforeupdate ruby-on-rails-3

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

在Winform上是否存在用于C#ComboBox的BeforeUpdate

我来自VBA世界,记得BeforeUpdate我可以在组合框上打电话.现在我在C#(并喜欢它),我想知道是否有BeforeUpdate一个ComboBox关于Winform 的呼吁?

我可以创建一个不可见的文本框并存储我需要的信息,在更新后,查看我需要的那个框,但我希望有一个更简单的解决方案.

c# combobox beforeupdate winforms

3
推荐指数
2
解决办法
8618
查看次数

如何使用Rspec和FactoryGirl测试rails模型中的before_update回调?

我试图测试下面的模型的before_update回调.

车型/ option.rb:

class Option < ApplicationRecord
  belongs_to :activity

  has_many :suboptions, class_name: "Option", foreign_key: "option_id"

  belongs_to :parent, class_name: "Option", optional: true, foreign_key: "option_id"

  accepts_nested_attributes_for :suboptions, allow_destroy: true,
    reject_if: ->(attrs) { attrs['name'].blank? }

  validates :name, presence: true

  before_create :set_defaults
  before_update :set_updates


  def set_defaults
    self.suboptions.each do |sbp|
      sbp.activity_id = self.activity_id
    end
  end

  def set_updates
    suboptions.each do |child|
      child.activity_id = self.activity_id
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

规格/型号/ option.rb:

require 'rails_helper'

RSpec.describe Option, type: :model do

  describe "Callbacks" do
    it "before_create" do
      suboption = create(:option)
      option …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails callback beforeupdate factory-bot

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

在使用Spock的Grails中测试beforeUpdate或beforeInsert

我是Grails的新手,我正在做一些测试,但是虽然在开发中调用了beforeUpdate和beforeInsert,但我的测试表明它们不是,我做错了什么?

我正在嘲笑Cicle和Measurement,所以我认为当调用方法save时,会触发beforeUpdate或beforeInsert,但是当我运行测试时,grails回答saing"调用太少:1*cicle.updateCicleValue()(0调用)"

所以我使用"何时"错误?或者save不会在mock对象中触发beforeUpdate和beforeInsert?

请帮忙 :)

Cicle.goovy

class Cicle {

String machine
double cicleValue

static hasMany = [measurements:Measurement]

def beforeInsert(){
    if (measurements != null) updateCicleValue()
}

def beforeUpdate(){
    if (measurements != null) updateCicleValue()
}

public void updateCicleValue(){

    double sumCicleValue = 0

    measurements.each{ measurement ->
        sumCicleValue += measurement.cicleValue
    }

    cicleValue = sumCicleValue / measurements.size()
}   
}
Run Code Online (Sandbox Code Playgroud)

CicleSepc.groovy

@TestFor(Cicle)
@Mock([Cicle, Measurement])
class CicleSpec extends Specification {

Measurement mea1    
Measurement mea2    
Cicle cicle


def setup() {
    mea1 = new Measurement(machine: "2-12", cicleValue: 34600)      
    mea2 …
Run Code Online (Sandbox Code Playgroud)

grails groovy beforeupdate spock

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