轨道:为每个has_one关联创建一个模型



我有一个新的模型settings,它有一个到另一个模型school的外键,其中已经存在许多schoolssettings belongs_to schoolschool has_one setting.如何一次为每个现有school制作一个setting

基本上你想要的是为没有设置的现有学校设置默认设置?

您可以执行以下操作:

School.all.each do |s|
unless s.settings.present? 
s.settings.new(your default settings here for each field of settings)
s.settings.save
end
end

我正在遍历每所学校,但可能有一种方法可以只抓取没有设置的学校。

您可以在控制台中运行它。(不过先在本地做(

最新更新