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

时间:2011-05-13 06:59:14

标签: ruby-on-rails ruby-on-rails-3 controller save

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调用,因为调用是动态的)它显然会提取最新的关联图像,而不是相关的图像。无论如何,我可以让对象获得在它制作时关联的图像吗?

3 个答案:

答案 0 :(得分:0)

我认为您需要对象的深层复制,因为正常关联(=)只会创建一个引用:

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

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

答案 1 :(得分:0)

我不知道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方法,或其他您尚未显示的内容。

答案 2 :(得分:0)

我找到了一种方法来做一些现在有用的事情。这不是最好的方式,但它现在有效。我基本上创建了一个空数组并将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