简体   繁体   English

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

[英]How do i get the before_save value in the controller in Rails 3?

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

The function share_associations has the parameters @product_category and product_category_was. 函数share_associations具有参数@product_category和product_category_was。 The problem is, when i call product_category_was.send('images') for example (which i have to call using send since the call is dynamic) it obviously pulls the newest associated images and not the images that were associated. 问题是,例如,当我调用product_category_was.send('images')时(由于调用是动态的,因此我必须使用send来调用它)显然会拉到最新的关联图像,而不是关联的图像。 Is there anyway i can get the object to get the images that were associated at the point in time it was made? 无论如何,我能否获得对象以获取在创建该对象时关联的图像?

I think you need a deep copy of your object, because normal association ( = ) will only create a reference: 我认为您需要对象的深层副本 ,因为普通关联( = )仅会创建一个引用:

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

This might not work for all kinds of objects, but for normal Rails objects this should work. 这可能不适用于所有类型的对象,但是对于普通的Rails对象而言,这应该起作用。

I have no idea what arnep is talking about, nor what problem you're getting. 我不知道arnep在说什么,也不知道您遇到了什么问题。 What you're doing works for me on two different finds through an association, and so it should. 通过关联,您正在做的事情对我有两个不同的发现,因此应该如此。

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> 

In fact, usually people are asking the inverse question - how to get an already instantiated object to reload from the DB. 实际上,通常人们会问一个相反的问题-如何获取已实例化的对象以从DB重新加载。 The problem is probably in your share_associations method, or something else you're not showing yet. 问题可能出在您的share_associations方法中,或者您尚未显示的其他内容。

I found a way to do something that works for now. 我找到了一种方法,现在可以工作。 It's not the greatest way, but it works for now. 这不是最好的方法,但目前可以使用。 I basically created an empty array and pushed the product_categories array into it. 我基本上创建了一个空数组,并将product_categories数组推入其中。 This made it so the value was no longer a call so the value does not change. 这样就可以使该值不再是调用,因此该值不会更改。 Hopefully this will help someone else eventually. 希望这最终会帮助别人。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM