【发布时间】:2017-03-21 15:18:19
【问题描述】:
我有一个Campaign和一个Material模型。
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
Material有一个属性code,我想用包含相关Campaign的id的链接填充这个属性code,在创建它时。
如何在 Material 模型中获取对象Campaign?
更新
对不起,我解释得不是很好。在上面的 Material 模型中,我想在 “创建活动过程”
期间获取父 ID 以填充代码属性【问题讨论】:
标签: ruby-on-rails ruby activerecord model nested-forms