不會傳播我有一個嵌套的模型設定:更新的嵌套模型
class Event < ActiveRecord::Base
belongs_to :place
:place
attr_accessible :place_attributes, :reject_if => :all_blank, :allow_destroy => false
class Place < ActiveRecord::Base
has_many :events
validates :label, :presence => true,
:uniqueness => {:case_sensitive => true, :on => :create }
validates :description, :presence => {:on => :create},
:uniqueness => {:case_sensitive => true , :on => :create}
在測試場景中,W嵌套形式,用戶可以只更新將#標籤屬性,保留所有的其他信息。 。
test "should_update_event_place_data" do
put :update, :locale => I18n.locale, :id => @event[:id],
:event => { :place_attributes => { label: "a very beautiful place" } }
這導致到EventsController#更新的請求,接收所述參數:
params
{"event"=>{"place_attributes"=>{"label"=>"a very beautiful place"}}, "locale"=>"en",
"id"=>"145", "controller"=>"backoffice/events", "action"=>"update"}
(rdb:1) @event.update_attributes(params[:event])
false
@messages={:"place.description"=>["cannot be blank"]
但驗證是在創建,而不是更新....沒有驗證錯誤應該被檢測到.. 什麼可能是錯的?
感謝您的幫助
I did more testing
debugger , right after the test setup (before sending the put request)
@event_0
#<Event id: 161, account_id: 3, place_id: 249, slug: "my-new-event-on-2013-01-01-at- edinburgh-united-king...", title: "My New Event"
@event_0.place
#<Place id: 249, label: "new fake place",..
test request:
put :update, :locale => I18n.locale, :id => @event_0[:id], :event => { :place_attributes => { label: "a very beautiful place"} }
params in request are OK, @request/method = PUT
In EventsController#update
@event.update_attributes(params[:event])
.... I inserted a debug in the Place model...
(before_validation :i_am_on_create, :on => :create)
def i_am_on_create
debugger
p "CREATING"
end
and it's creating !! don't understand why it's not updating the parent nested model
你可以發佈測試和相應的控制器代碼的其餘代碼? –
你確定關聯的'description'已經存在嗎?沒有看到你的控制器代碼是不可能的,但是在我看來,雖然「@ event」已經存在,但是當你更新父事件記錄時,會創建「@ event.description」並觸發驗證。只是一個猜測。 –