Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

What to do when generating a model with double belongs_to ? #5

Open
bmedici opened this issue Jan 7, 2010 · 1 comment
Open

What to do when generating a model with double belongs_to ? #5

bmedici opened this issue Jan 7, 2010 · 1 comment

Comments

@bmedici
Copy link

bmedici commented Jan 7, 2010

Let's say we have Book, Shelf and Author models. We can generate Authors and Books inside the loop, but how could we link Shelves?

Shelf.populate 20 do |shelf|
shelf.name = Populator.words(1..3).titleize
end
Author.populate 40 do |author|
Book.populate 0..40 do |book|
book.author_id = author.id
end
end

Any idea or suggestion ?

@Tornskaden
Copy link

I can see this is an old issue, yet still open.
Here is my suggestion to the problem

Shelf.populate 20 do |shelf|
  shelf.name = Populator.words(1..3).titleize
end
Author.populate 40 do |author|
  Book.populate 0..40 do |book|
    book.author_id = author.id
    book.shelf_id = Shelf.offset(rand(Shelf.count)).limit(1)
  end
end

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants