1

当我尝试使用 db/seeds.rb 文件预填充数据库时,我的代码如下所示:

 now = Time.now.beginning_of_hour
later = Time.now.end_of_hour + 1
sch1 = Schedule.create(start_time: now, stop_time: later) 
sch1.channel = channel
sch1.program = cartoon
cartoon.schedules.push(sch1)
sch1.save
cartoon.save

其中 channel 和 cartoon 是在前面的代码中使用 Channel.create 和 Program.create 定义的,并且具有适当的值。

当我查看 rails 控制台时,我看到所有三个对象(频道、节目和时间表)都存在,但没有时间表没有 program_id。(schedule.program 出于某种原因工作得很好)。程序的 program.schedules 也是一个空数组。

作为测试,我在控制台中执行了以下操作:

now = Time.now.beginning_of_hour
later = Time.now.end_of_hour + 1
channel = Channel.first
cartoon = Program.first
sch1 = Schedule.create(start_time: now, stop_time: later) 
sch1.channel = channel
sch1.program = cartoon
cartoon.schedules.push(sch1)
sch1.save
cartoon.save

这工作得很好。

种子有什么特别之处?我需要在稍后引用它之前调用 save 吗?该变量显然不为空...

4

0 回答 0