Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top