Blook

published: 3/13/2019last updated: 4/10/2019

Create model and Dummy data

*Goal of this chapter

  • Create User and Post model

  • Create dummy data

*This chapter is not related with graphql-ruby!

1. Create User

rails g model User name image
rails db:migrate

2. Create Post

rails g model Post title body user:references
rails db:migrate

3. Edit user.rb

# == Schema Information # # Table name: users # # id :bigint(8) not null, primary key # image :string # name :string # created_at :datetime not null # updated_at :datetime not null # class User < ApplicationRecord
validates :name, presence: true # this is up to you has_many :posts, dependent: :destroy end

4. Edit post.rb

This is validation is up to your use-case.

# == Schema Information # # Table name: posts # # id :bigint(8) not null, primary key # body :string # title :string # created_at :datetime not null # updated_at :datetime not null # user_id :bigint(8) # # Indexes # # index_posts_on_user_id (user_id) # # Foreign Keys # # fk_rails_... (user_id => users.id) # class Post < ApplicationRecord validates :title, presence: true validates :body, presence: true belongs_to :user end

5. Create dummy data

...
gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master'

bundle

rails c
User.create(name: Faker::Name.name, image: Faker::Avatar.image)
Post.create(title: Faker::Book.title, body: Faker::Lorem.paragraph, user_id:1)

Reference