DEV Community

disanchez27edu
disanchez27edu

Posted on

Creating Records with Associations

To create my sample data, I simply created a new set of records for each table. I included logic so that the entities that had any kind of association would be assigned a user through the necessary foreign_key.

 ## SAMPLE PHOTOS ###

  p "Creating sample photos"

  100.times do
    new_photo = Photo.create(
     owner_id: User.all.sample.id,
     caption:  Faker::Hipster.sentence(word_count: 10),
     image:   "http://www.robohash.org/"+Faker::Number.number(digits: 4).to_s
    )
  end

  p "#{Photo.count} photos created."
  p "Here's an example:"
  p Photo.all.order(id: :desc).first

Enter fullscreen mode Exit fullscreen mode

Yet in my database, those associations are not understood. Why is this?

Top comments (0)