0
基於刪除我有一個的has_many:通過關聯如下軌道4的has_many:通過級聯的複選框值
Customer->
has_many :customer_materials, :dependent => :destroy
has_many :materials, :through => :customer_materials
CustomerMaterial -> #This is the lookout table for a price attribute
belongs_to :customer
belongs_to :material
Material ->
has_many :customer_materials, :dependent => :destroy
has_many :customers, :through => :customer_materials
並非所有的客戶通過查找表有這樣的自訂價格。只有當客戶的custom_price布爾值被選中時,我才使用查找表。如果custom_price布爾值爲false,則使用Material表中由price屬性跟蹤的默認價格。
當我編輯客戶並取消選中custom_price複選框時,我想繼續並在查找表上觸發級聯刪除。這是我更新的控制器方法。如何修改方法以基於custom_price屬性進行條件更新?
def update
@customer = Customer.find(params[:id])
if @customer.update_attributes(customer_params)
flash[:success] = "Record Updated Successfully"
redirect_to customers_url
else
flash.now[:error] = @customer.errors.full_messages.to_sentence
render 'edit'
end
end
我可能得到一個錯誤,因爲在Postgres裏,一個布爾值是我應該改變這種屬性爲整數存儲爲TRUE,而不是1和FALSE而不是0?編輯:這是custom_pricing複選框的值。 – ctilley79
你的錯誤是什麼? – usha
原因在渲染編輯視圖時,這是一個不同的問題。我將不得不使用一些JavaScript來設置複選框的值。除了將custom_prices.delete_all更改爲customer_materials.delete_all之外,您的解決方案可以正常工作 – ctilley79