2017-09-13 34 views
0

我使用的是Rails 5,並且在使用accept_nested_attributes_for創建一個雙重嵌套記錄時遇到了問題。我有模型,看起來像這樣:使用accept_nested_attributes_for來創建一個雙軌嵌套的記錄使用rails

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工作正常,甚至Rebate和MoneyFactor嵌套在Term下(我將這些因爲他們工作正常而離開這些人), ,但我在嘗試創建Residual時遇到了困難,因爲它屬於兩個不同的屬於報價的模型。我已經閱讀了許多關於nested_attributes_for的文章,但似乎沒有處理這種特定的情況。

我QuotesController創建行動:

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:

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 

我使用與終極版陣營的前端,這是我的看法長相如:

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。你有兩個選擇:設置兩個一對多的關係或使用多態。假設殘差已經有term_idmileage_id列,您可以在TermMileage中添加has_many :residuals

Polymorphic: 這使您可以設置一個界面,以便Residual與任何數量的模型一起使用。

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提供了一個has_many與Residual的關係,但是當我嘗試保存到db時,我得到了「列中的空值」mileage_id「違反了非null約束」。我相信的原因是,在Term下嵌套時創建的剩餘記錄不知道里程記錄,因此沒有其ID。 –

+0

另外,如果我沒有弄錯,當使用多態關聯時,每個剩餘只會被分配到一個里程或一個期限,但在我的情況下,剩餘總是具有特定的里程和期限。 –

+0

如果你希望你的模型屬於多個模型,你是對的,多態在這裏不起作用。你只需要使用多個'belongs_to'。關於你的錯誤,你可以展示你的表單和控制器嗎? – EJ2015

相關問題