-1

I want to push dummy content in my database. There is a 1:n relationship concepted in my models.

seeds.rb:

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

I try this

city << person

and

person.city_id = city.id

How would you do this?

sigod
  • 3,514
  • 2
  • 21
  • 44
ubuseral
  • 427
  • 1
  • 3
  • 14

1 Answers1

1

Your question is a bit vague. It seems like you have a relationship where a city has many people. If so, then you can assign the relationship like this:

city.people << person

or this

person.city_id = city.id
person.save

This method requires save since assigning the city_id to person does not write the change to the database.

You can try these out in your rails console

claptimes
  • 1,615
  • 15
  • 20