我有三種模型:Deal,Zipcode,DealIncludeZipcode。Ruby on Rails嵌套表格
現在,聯想看起來象下面這樣: -
新政型號:
class Deal < ActiveRecord::Base
has_many :deal_include_zipcodes, dependent: :destroy
has_and_belongs_to_many :zipcodes, dependent: :destroy
accepts_nested_attributes_for :deal_include_zipcodes,:reject_if => :reject_include_zipcodes, allow_destroy: true
private
def reject_include_zipcodes(attributes)
if attributes[:deal_id].blank? || attributes[:zipcode_id].blank?
if attributes[:id].present?
attributes.merge!({:_destroy => 1}) && false
else
true
end
end
end
end
class Zipcode < ActiveRecord::Base
has_and_belongs_to_many :deals
end
class DealIncludeZipcode < ActiveRecord::Base
belongs_to :deal
belongs_to :zipcode
end
鑑於我有一個複選框,取消勾選上它,我可以選擇多個郵政編碼從DealIncludeZipcode.But時選擇
現在我保存它沒有保存的數據。
我已經使用遷移加入郵編和交易模型,其中我的排除郵政編碼功能工作正常。
請提供一個soloution.I已經嘗試了各種方法,但沒有成功。
我知道這一點,我用它們來排除郵編。 –
我有一個包含郵編的功能。用戶可以通過選擇適用於Jquery的郵編來使用包含和排除郵編。在哪裏以及如何保存包含郵政編碼的數據作爲聯合表具有排除郵政編碼的數據。 –
我仍然認爲你不明白 - 你的代碼根本不使用DealIncludeZipcode模型,因爲關係被定義爲錯誤的。 – max