2011-05-13 6 views
1
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 

function share_associations에는 @product_category 및 product_category_was 매개 변수가 있습니다. 문제는, 예를 들어 product_category_was.send ('images')를 호출하면 (호출이 동적이기 때문에 send를 사용하여 호출해야 함) 분명히 연결된 최신 이미지가 아닌 연결된 이미지를 가져옵니다. 어쨌든 내가 만든 객체가 그 시점에 연결된 이미지를 얻을 수 있습니까? 내가 생각레일즈 3의 컨트롤러에서 before_save 값을 얻으려면 어떻게해야합니까?

+0

구체적으로 말하십시오 – Hitesh

+0

아마도 당신이하고있는 일을 이해하기 위해 모델 코드를 볼 필요가 있습니다. –

답변

0

당신은 정상 협회 (=)는 참조 만 만들 수 있기 때문에, 개체의 딥 카피이 필요합니다

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

이 개체의 모든 종류의 작동하지 않을 수 있습니다,하지만 일반 레일에 대한 이 객체가 작동해야합니다.

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> 

실제로 사람들은 역 의문을 제기하고 있습니다. 이미 인스턴스화 된 객체를 DB에서 다시로드하는 방법입니다. 문제는 아마도 share_associations 방법이거나 아직 표시하지 않는 것일 수 있습니다.

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 
관련 문제