ruby on rails - Polymorphic has_one built through accepts_nested_attributes_for is not setting polymorphic type -


note: while project uses spree version 2.3, not of belief spree-specific problem. though please correct me if wrong.

the spree framework has model called calculator looks this:

module spree   class calculator < spree::base     belongs_to :calculable, polymorphic: true      ...   end end 

i inheriting class create own calculator looks (which little different other spree calculator subclass):

module spree   class calculator     class percentdiscountonvariant < calculator       preference :percent, :decimal, default: 0        ...     end   end end 

my model, called clientproduct has has_one relationship calculator, , can accept nested attributes it, so:

module spree   class clientproduct < activerecord::base     has_one :calculator, inverse_of: :calculable, foreign_key: "calculable_id", dependent: :destroy      accepts_nested_attributes_for :calculator      ...    end end 

the problem when create clientproduct (either new record, or updating existing), calculable_type column in calculators table remains null. however, calculable_id populated correctly clientproduct's id.

the relevant portion of parameter map is:

"client_product"=>{     "variant_id"=>"300",      "client_id"=>"2",      "role_ids"=>["7"]     "calculator_attributes"=> {         "type"=>"spree::calculator::percentdiscountonvariant",         "preferred_percent"=>"15"     } } 

and clientproduct created spree::clientproduct.create(client_product_params).

what cause polymorphic id set correctly, while simultaneously leaving polymorphic type column null?

minor sidenote: lying bit simplicity , brevity's sake regarding how clientproduct built. multiple clientproduct rows mass inserted, using combinations variant_ids , client_ids. however, calculator_attributes same each clientproduct created, not believe particular setup changes anything. however, if feels might relevant, let me know , provide actual (though longer) code.

not sure if cause, left out polymorphic part in other side of relation ( has 1 side )

has_one :calculator,   inverse_of: :calculable,   foreign_key: :calculable_id,   dependent: :destroy,   as: :calculable        #  <== part 

Popular posts from this blog