Ich gab auf den Versuch, den autosave
Parameter zu überschreiben, da ich denke, dass es nicht getan werden kann.
zog ich die has_shipping_address
vom Order
zum ShippingAddress
Modell und jetzt habe ich:Schienen access_nested_attributes_for: reject_if funktioniert nicht
#the models..
class Order < ActiveRecord::Base
belongs_to :billing_address
belongs_to :shipping_address
accepts_nested_attributes_for :billing_address
accepts_nested_attributes_for :shipping_address, :reject_if => proc { |attributes| attributes["has_shipping_address"] != '1' }
def after_initialize
self.build_billing_address unless billing_address
self.build_shipping_address unless shipping_address
end
end
class ShippingAddress < OrderAddress
attr_accessor :has_shipping_address
end
class OrderAddress < ActiveRecord::Base
validates_presence_of :name
#more validations here..
end
#the view
<% form_for @order do |f| %>
#...
<% f.fields_for :shipping_address do |addr_f| %>
<%= addr_f.check_box :has_shipping_address %>
<%= addr_f.text_field :name %>
#more fields for the address..
<% end %>
<% end %>
Das Problem ist, dass :reject_if
nicht zu tun, scheint es die Aufgabe. Unabhängig davon, was der Wert für has_shipping_address
ist, wird die save
-Methode immer noch auf dem verschachtelten ShippingAddress
aufgerufen, was zu Validierungsfehlern führt.
Mache ich hier etwas falsch? Das wird ein bisschen frustrierend.