我如何在 Rails 3 的控制器中获取 before_save 值?

Posted

技术标签:

【中文标题】我如何在 Rails 3 的控制器中获取 before_save 值?【英文标题】:How do i get the before_save value in the controller in Rails 3? 【发布时间】:2011-05-13 06:59:14 【问题描述】:
def update
    @product_category = @business_category.product_categories.find(params[:id])
    product_category_was = @business_category.product_categories.find(params[:id])

    respond_to do |format|
      if @product_category.update_attributes(params[:product_category])
        share_associations(@product_category, product_category_was) if in_params('_maps_attributes', 'product_category')

        format.js
        format.html  redirect_to(admin_product_categories_path, :notice => 'Product category was successfully updated.') 
        format.xml   head :ok 
      else
        format.js
        format.html  render :action => "edit" 
        format.xml   render :xml => @product_category.errors, :status => :unprocessable_entity 
      end
    end
  end

函数share_associations 具有参数@product_category 和product_category_was。问题是,例如,当我调用 product_category_was.send('images') 时(由于调用是动态的,我必须使用 send 调用)它显然会提取最新的关联图像,而不是关联的图像。无论如何我可以让对象获取在制作时关联的图像吗?

【问题讨论】:

可能需要查看模型代码才能了解您在做什么。 【参考方案1】:

我认为您需要对象的深拷贝,因为正常关联 (=) 只会创建引用:

product_category_was = Marshal::load(Marshal::dump(@product_category))

这可能不适用于所有类型的对象,但对于普通的 Rails 对象应该可以。

【讨论】:

【参考方案2】:

我不知道 arnep 在说什么,也不知道你遇到了什么问题。你正在做的事情对我来说是通过关联在两个不同的发现上工作的,所以它应该。

irb(main):016:0> s = School.first
=> #<School id: 2, name: "Bar", created_at: "2011-04-09 17:48:57", updated_at: "2011-05-13 09:13:38", confirmed: nil, zipcode: nil>
irb(main):017:0> g1 = s.grades.find 4
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17">
irb(main):018:0> g2 = s.grades.find 4
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17">
irb(main):019:0> g1.update_attributes :name => '5th'
=> true
irb(main):020:0> g2
=> #<Grade id: 4, name: "4th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:15:17">
irb(main):021:0> g1
=> #<Grade id: 4, name: "5th", type: nil, school_id: 2, created_at: "2011-04-19 03:17:49", updated_at: "2011-05-13 09:16:02">
irb(main):022:0> 

事实上,人们通常会问相反的问题——如何让已经实例化的对象从数据库中重新加载。问题可能出在您的 share_associations 方法中,或者您尚未显示的其他问题上。

【讨论】:

【参考方案3】:

我找到了一种方法来做一些现在可行的事情。这不是最好的方法,但它现在有效。我基本上创建了一个空数组并将 product_categories 数组推入其中。这使得它的值不再是一个调用,所以值不会改变。希望这最终会对其他人有所帮助。

def update
    @product_category = @business_category.product_categories.find(params[:id])
    if in_params('_maps_attributes', 'product_category')
      media_was = Array.new
      media_was = media_was | @business_category.product_categories.find(params[:id]).send(map_type('product_category').pluralize)
    end

    respond_to do |format|
      if @product_category.update_attributes(params[:product_category])
        share_associations(@product_category, media_was) if in_params('_maps_attributes', 'product_category')

        format.js
        format.html  redirect_to(admin_product_categories_path, :notice => 'Product category was successfully updated.') 
        format.xml   head :ok 
      else
        format.js
        format.html  render :action => "edit" 
        format.xml   render :xml => @product_category.errors, :status => :unprocessable_entity 
      end
    end
  end

【讨论】:

以上是关于我如何在 Rails 3 的控制器中获取 before_save 值?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ruby​​ on rails 中获取控制器和操作列表?

如何获取存储在 Rails 控制器的活动存储中的附件的 url

Rails 3 - 获取一个字段的完整错误消息

如何在 Rails 控制台中获取执行时间?

如何在 Rails 2.3 中的 .each 循环中排序

如何将AngularJS $资源中的params序列化为rails 3.2兼容的参数