ruby on rails - Using :reject_if => :all_blank on double nested forms -



ruby on rails - Using :reject_if => :all_blank on double nested forms -

so have next models

pet class pet < activerecord::base # associations has_and_belongs_to_many :owners accepts_nested_attributes_for :owners, :reject_if => :all_blank end owner class owner < activerecord::base # associations has_and_belongs_to_many :pets has_one :phone_number, :as => :callable, :dependent => :destroy has_one :address, :as => :addressable, :dependent => :destroy accepts_nested_attributes_for :phone_number accepts_nested_attributes_for :address end phone number class phonenumber < activerecord::base belongs_to :callable, polymorphic: true end address class address < activerecord::base belongs_to :addressable, polymorphic: true end

i have top level form_for @pet f.fields_for :owners nested within , f.fields_for :phone_number , f.fields_for :address both nested within f.fields_for :owners block. means params.require looks this.

params.require(:pet).permit(:name, :breed, :color, :neutered, :microchip, :flee_control, :heartworm_prevention, :date_of_birth, :gender, :species_id, :avatar, :owners_attributes => [ :first_name, :last_name, :email, :phone_number_attributes => [:number], :address_attributes => [:line1, :city, :state, :zip_code] ])

my params right , can create new records , works fine. issue comes when seek utilize :reject_if => :all_blank reject blank owners.

because there sec level of nested attributes, phone_number_attributes , address_attributes both considered not_blank technically of type !ruby/hash:actioncontroller::parameters, allows object built blank attributes when shouldn't be.

i've been searching 2 hours , can't find mention of issue. missing obvious? i've tried adding :reject_if => :all_blank on owner model phone number , address no luck going route either.

edit: able work there must improve built-in way this.

accepts_nested_attributes_for :owners, reject_if: proc { |attributes| attributes.all? |key, value| if value.is_a? actioncontroller::parameters value.all? { |nested_key, nested_value| nested_key == '_destroy' || nested_value.blank? } else key == '_destroy' || value.blank? end end }

ruby-on-rails ruby

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -