Using accepts_nested_attributes_for to create a double nested record with rails -


i'm using rails 5 , i'm having trouble creating doubly nested record using accepts_nested_attributes_for. have models looks this:

class quote < applicationrecord   has_many :terms, inverse_of: :quote   has_many :mileages, inverse_of: :quote end  class term < applicationrecord   belongs_to :quote, optional: true end  class mileage < applicationrecord   belongs_to :quote, optional: true end  class residual < applicationrecord   belongs_to :term, optional: true   belongs_to :mileage, optional: true end 

and trying use accepts_nested_attributes_for create residual record when creating new quote. working fine term , mileage, , rebate , moneyfactor nested under term (i leaving out because working fine), having difficulty when trying create residual because belongs 2 different models belongs quote. i've read many posts nested_attributes_for none seem deal specific situation.

my quotescontroller create action:

  def create     @quote = quote.new(quote_params)     @quote.user_id = current_user.id      if @quote.save       @quotes = current_user.quotes.includes(:terms, :rebates,         :money_factors, :residuals, :mileages)        render :index     else      render json: @quote, status: 422     end   end 

here strong params in quotes controller:

  def quote_params     params.require(:quote).permit(:user_id, :lead_id, :year, :make,       :make_id, :model, :model_id, :trim, :trim_id, :title, :msrp, :sell_price, :profit, :customer_cash, :bank_fee_plan,   :registration_plan, :smog_plan, :misc_fee_plan, :rebate_tax_plan, :doc_fee_plan,   :down_payment, :drive_off, :monthly_payment, :tax, :bank_fee, :registration, :doc_fee, :smog,   :misc_fee, :rebate_tax,    mileages_attributes: [:id, :quote_id, :mileage,     residual_attributes: [:id, :term_id, :mileage_id, :residual]   ],   terms_attributes: [     :id,     :months,     rebates_attributes: [:id, :term_id, :amount],     money_factors_attributes: [:id, :term_id, :money_factor],     residuals_attributes: [:id, :term_id, :mileage_id, :residual]   ] )   end 

i using react redux on front end view looks :

const quote = merge({}, this.state, {   terms_attributes: [{     months: this.state.months,     rebates_attributes: [{ amount: this.state.rebate }],     money_factors_attributes: [{ money_factor: this.state.money_factor }],     residuals_attributes: [{ residual: this.state.residual }]   }],   mileages_attributes: [{ mileage: this.state.mileage, residuals_attributes: [{ residual: this.state.residual }] }], }); 

here full quote, term, mileage, , residual models:

class quote < applicationrecord   validates :user_id, presence: true   belongs_to :user  belongs_to :lead, optional: true   has_many :mileages, dependent: :destroy, inverse_of: :quote   has_many :terms, dependent: :destroy, inverse_of: :quote   has_many :rebates,   through: :terms,   source: :rebates   has_many :money_factors,   through: :terms,   source: :money_factors   has_many :residuals,   through: :mileages,   source: :residuals   has_many :residuals,   through: :terms,   source: :residuals    accepts_nested_attributes_for :mileages, :terms, allow_destroy: true  end  class term < applicationrecord   validates :months, presence: true   belongs_to :quote, optional: true   has_many :rebates, dependent: :destroy  has_many :money_factors, dependent: :destroy  has_many :residuals, dependent: :destroy   accepts_nested_attributes_for :rebates, :money_factors, :residuals,  allow_destroy: true  end  class mileage < applicationrecord   validates :mileage, presence: true   belongs_to :quote, optional: true   has_many :residuals, dependent: :destroy   accepts_nested_attributes_for :residuals, allow_destroy: true end  class residual < applicationrecord   validates :residual, presence: true   belongs_to :term, optional: true  belongs_to :mileage, optional: true end 

you can still use #accepts_nested_attributes_for. you've got 2 options here: set 2 one-to-many relationship or use polymorphic. assuming residual has term_id , mileage_id columns, can add has_many :residuals in term , mileage.

polymorphic: enables set 1 interface residual work number of models.

class term < applicationrecord   belongs_to :quote, optional: true   has_many: residuals, as: :residualable  end  class mileage < applicationrecord   belongs_to :quote, optional: true   has_many: residuals, as: :residualable end  class residual < applicationrecord   belongs_to :residualable, polymorphic: true, optional: true end 

make sure have residualable_id , residualable_type columns in residuals table.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -