2011-12-15 2 views
-1

중첩 된 필드의 두 수준으로 모델을 업데이트하는 중 문제가 발생했습니다. 올바른 값을 올바르게 개체를 업데이트 할 콘솔 : (travels_attributes => {...}) 내가 flight.update_attributes를 호출 할 때 개체를 업데이트 할 때 중첩 된 특성이 저장되지 않습니다.

내가

지금 아주 간단한 모델

class Flight < Plan 
    attr_accessible :travels_attributes 
    has_many :travels, :class_name => "FlightTravel", :foreign_key => "plan_id", :dependent => :destroy 
    accepts_nested_attributes_for :travels, :allow_destroy => true 
End 

class FlightTravel < ActiveRecord::Base 
    attr_accessible :segments_attributes 

    has_many :segments, :class_name => "FlightSegment", :dependent => :destroy, :foreign_key => "flight_travel_id" 
    accepts_nested_attributes_for :segments, :allow_destroy => true 
end 

class FlightSegment < ActiveRecord::Base 

end 

있습니다.

나는 flight.save를 호출하고 아무 것도하지 않으며, 어떤 이유로 든 중첩 된 연결을 업데이트하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까 ?

+0

실패 할 수있는 유효성 검증이 있습니까? 아마도 단순화 된 데이터로 인해 실패한 실제 통화를 보는 데 도움이됩니다. 콘솔에서 update_attributes를 호출하면 데이터베이스에 저장해야합니다. 나는 다음과 같이 가정한다 : travel_attributes [sic]는 키가 오타이다. – austinfromboston

+0

죄송하지만 travels_attributes가 있어야합니다. 업데이트했습니다. 아니요, 유효성 검사가 아닙니다. 왜냐하면 FlightSegment 클래스의 before_validation 콜백을 수행하지 않기 때문입니다 –

+0

레일 콘솔에서 이것을 수행 한 다음 #save 후에 flight.errors가 비어 있는지 확인할 수 있습니까? –

답변

0

나는 문제가 있음을 알았습니다.

나는 내 수업 실제로 나는 FlightTravel 세그먼트가 여행 세그먼트를 대체 할 기다리고 있었다이

class Flight < Plan 
    attr_accessible :travels_attributes 
    has_many :travels, :class_name => "FlightTravel", :foreign_key => "plan_id", :dependent => :destroy 
    accepts_nested_attributes_for :travels, :allow_destroy => true 
End 

class Travel < ActiveRecord::Base 
    attr_accessible :segments_attributes 
    has_many :segments, :class_name => "TravelSegment", :dependent => :destroy, :foreign_key => "flight_travel_id" 
    accepts_nested_attributes_for :segments, :allow_destroy => true 
end 

class FlightTravel < Travel 
    attr_accessible :segments_attributes 

    has_many :segments, :class_name => "FlightSegment", :dependent => :destroy, :foreign_key => "flight_travel_id" 
    accepts_nested_attributes_for :segments, :allow_destroy => true 
end 

class FlightSegment < ActiveRecord::Base 

end 

처럼 배치했지만, 어떤 이유로 그렇지 않습니다. 레코드를 만들 때 작동하지만 업데이트하려고 할 때는 작동하지 않습니다.

관련 문제