【问题标题】:Rails: Seeds, RelationshipRails:种子,关系
【发布时间】:2013-02-17 23:36:00
【问题描述】:

我想在我的数据库中推送虚拟内容。我的模型中有一个 1:n 的关系概念。

seeds.rb:

city= City.create([{:ort_name=>"Hagen"}])
person= Person.create([{:name = "root"}])

我试试这个

city << person

person.city_id = city.id

你会怎么做?

【问题讨论】:

  • 也许有人知道?

标签: ruby-on-rails seed


【解决方案1】:

你的问题有点含糊。看起来你有一个城市有很多人的关系。如果是这样,那么您可以像这样分配关系:

city.people << person

或者这个

person.city_id = city.id
person.save

此方法需要save,因为将city_id 分配给person 不会将更改写入数据库。

您可以在rails console 中尝试这些

【讨论】:

  • 我都尝试了:我得到未定义的方法 people 或 id
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多