我hava a 活動和材料模型。如何獲取子模型內的父對象ID?嵌套形式
class Campaign < ApplicationRecord
has_many :materials
accepts_nested_attributes_for :materials, reject_if: :all_blank, allow_destroy: true
end
和
class Material < ApplicationRecord
belongs_to :campaign
def code=(val)
new_code = val
suffixes = %w(.png .jpg .jpeg .gif .bmp)
urls = URI.extract(new_code, ['http', 'https'])
urls.each do |url|
new_code = new_code.gsub(url, "#{url}&var1=#{self.campaign.id}") unless url.ends_with? *suffixes #<--- (this is not working
end
write_attribute(:code, new_code)
end
end
材料有一個屬性碼,我想,以填補包含相關運動的ID的鏈接這個屬性代碼,同時創造它。
我怎樣才能得到對象運動裏面的材料模型?
UPDATE
對不起,我沒有很好地解釋。在上述材料模式,我想父ID來填充代碼屬性,在在「創建活動的過程」
對不起,我沒有很好地解釋我的問題,我剛剛更新了我的問題。 –
希望我的編輯能夠解決您的問題。 – SteveTurczyn