2011-12-15 11 views
-1

問題を解決しようとしています。ネストされたフィールドの2つのレベルの深いモデルを更新しています。 (: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 

のようにレイアウトされていたが、何らかの理由でそれはしていません。レコードの作成には使用できますが、更新するときには使用できません。

関連する問題