###前提・実現したいこと
rails 4.2.6で現在Facebookのクローンを作っています。ユーザーが投稿したTopicに対して、ほかのユーザーがCommentできる機能があります。そのコメントのseedデータを5件ほど作りたいのですが、すでにseedデータとして作ったユーザーIDとTopicのIDを使いたいと考えています。
すでにあるIDを指定してseedデータを作るとき、どのようにプログラムを組んだらいいか分からず詰まってしまったのでご教授願えませんでしょうか?
ちなみに、現在それぞれのアソシエーション関係はこのようになっております。
user.rb
'''
has_many :topics, dependent: :destroy
has_many :comments, dependent: :destroy
'''
topic.rb
'''
belongs_to :user
has_many :comments, dependent: :destroy
'''
comment.rb
'''
belongs_to :user
belongs_to :topic
'''
ユーザーとトピックのシードデータの作成の仕方はこうです
'''
5.times do |nam|
User.create(name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Internet.password,
avatar: Faker::Avatar.image,
uid: SecureRandom.uuid)
end
users = User.all
users.each do |user|
user.topics.create(content: Faker::Book.title)
end
'''
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。