2013-07-30 7 views
0

내가 같은 코드를 가지고 : 나는 또한 ...레일 업데이트 방법에 갱신 관련 모델 데이터

undefined method `update_attributes' for #<ActiveRecord::Relation:0x007f7fb4cdc220> 

을 내 other_products_cross_lists가 업데이트되지 않습니다

def update 
    @oil = Oil.find(params[:id]) 
    @product_types = ProductType.all  
    if @oil.update_attributes(params[:oil]) 
     if @oil.other_products_cross_lists.update_attributes(:cross_value => @oil.model.to_s.gsub(/\s+/, "").upcase) 
     redirect_to admin_oils_path 
     end 
    else 
     render :layout => 'admin' 
    end 
    end 

을하지만 난 그것을 실행할 때 내가 얻을 update_attribute를 시도하고 같은 오류가 발생합니다.

내가 뭘 잘못 했니? 내가 실행할 때

또한 내

def destroy 
    @oil = Oil.find(params[:id]) 
    if @oil.destroy 
     if @oil.other_products_cross_lists.destroy 
     redirect_to admin_oils_path 
     end 
    else 
     render :layout => 'admin' 
    end 
    end 

other_products_cross_lists 파괴하지 않은 방법 ...

가 어떻게이 문제를 해결할 수 파괴?

모델 :

class Oil < ActiveRecord::Base 
    has_many :other_products_cross_lists, :foreign_key => 'main_id' 

class OtherProductsCrossList < ActiveRecord::Base 
    belongs_to :oil 
+0

'오일'의 모델 정의를 게시 할 수 있습니까? –

+0

@MartinM 추가 한 –

답변

1

other_products_cross_lists은 오일 모델 협회입니다. Array 또는 ActiveRecord : Relation 객체에서 update_attributes를 사용할 수 없습니다. 당신이 무엇을해야

@oil.other_products_cross_lists.delete_all

또는

@oil.other_products_cross_lists.destroy_all 

당신을 사용할 수 있습니다

을 파괴하기위한

@oil.other_products_cross_lists.each {|list| list.update_attributes(:cross_value => @oil.model.to_s.gsub(/\s+/, "").upcase)} 

입니다 명확성을 위해 delete_all과 destroy_all의 차이점을 확인해야합니다.

+0

오케이) 어쩌면 아마도 another_products_cross_lists를 선택하기 위해 하나 더 찾기를 작성하는 것이 더 쉬울까요? –

+0

@oil 오브젝트에서 other_products_cross_lists를 호출하는 것은 완벽합니다. –

0

오류로 표시됩니다. other_products_cross_lists은 (귀하의 모델은 oil has_many other_products_cross_lists라고 가정합니다.

update_attribute은 관계의 방법이 아닌 모델 인스턴스의 한 방법입니다.

은 정말 당신이 update_attribute 수행 할 작업을 이해하지 않지만, 사용자 nested_attributes 경우, 다음

@oil.update_attributes(params[:oil]) 

는 관계를 갱신 처리한다.

또한 관계를 OilOtherProducts으로 정의하면 dependend: :destroy으로, 레일스는 종속 레코드의 리무버를 처리합니다.