2017-09-13 21 views
0

私はRails 5を使用していますが、accepts_nested_attributes_forを使用して二重にネストされたレコードを作成する際に問題があります。accepts_nested_attributes_forを使用してレールで二重ネストされたレコードを作成する

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 

そして、私は新しい見積もりを作成するときに残存レコードを作成するためにaccepts_nested_attributes_for使用しようとしています:私はこのようになりますモデルを持っています。 TermとMileage、さらにはTermの下にネバーされたRebateとMoneyFactor(それは正常に動作しているため、それらを残しています)、 が残っていますが、2つの異なる見積もりに属するモデル私はnested_attributes_forに関する多くの記事を読んだことがありますが、この特定の状況に対処するものはありません。ここで

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 

は私の引用コントローラに強いのparamsます:

私QuotesControllerはアクションを作成

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 

私は、フロントエンドに再来と反応して使用していますので、これはどのような私の見解のルックスでありますlike:

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 }] }], 
}); 

ここに私の完全な見積もり、期間、マイレージ、および残差モデルがあります:

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 
+0

'Term'モデルとMileageモデルでは、深くネストされた属性を使用する場合は、' Residual'モデルとの関連付けも必要です。 –

+0

コントローラとビューコードを追加できますか? –

答えて

0

#accepts_nested_attributes_forは引き続き使用できます。 2つの1対多の関係を設定するか、多態性を使用します。残差がすでにterm_idmileage_idの列であると仮定すると、TermMileagehas_many :residualsを追加できます。

多態性: Residualに1つのインターフェイスを設定して、任意の数のモデルで動作させることができます。

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 

あなたの残差テーブルにresidualable_idresidualable_type列を持っていることを確認してください。

+0

私はあなたの最初のアプローチを試してみました。TermとMileageの両方にResidualとのhas_many関係を付けましたが、dbに保存しようとすると、 "null値の列" mileage_id "not-null constraint"になります。私が信じる理由は、タームの下にネストされたときに残っているレコードがマイレージレコードを知らないので、そのIDを持っていないということです。 –

+0

また、私が間違っていない場合、多型関連を使用する場合、各残余はマイレージまたはタームのいずれかにのみ割り当てられますが、私の場合、残余は常に特定のマイレージとタームを持ちます。 –

+0

あなたのモデルが複数のモデルに属するようにするには、正しい多形がここでは機能しません。代わりに複数の 'belongs_to'を使用するだけです。あなたのエラーについては、フォームとコントローラを表示できますか? – EJ2015

関連する問題