[RUBY] Add, save, delete, add columns, save images from rails DB creation

Introduction

Create DB, add contents, save, delete with rails console.

Creating a User table

$ rails g model User name:string email:string
id name(string) email(string)
1 alice [email protected]
2 tom [email protected]

Reflect changes in database

$ rails db:migrate

Add data (C)

$ rails console
> user = User.new(name:"alice", email:"[email protected]")
> user.save

Read data (R)

> user = User.all
> user[0]

Data update (U)

> user = User.all
> user0 = user[0]
> user[0].name = "alice_alice"
> user[0].save

Delete data (D)

> user[0].destroy

Validation

Validation is a mechanism to verify whether the data is correct before the object is saved in the DB.

Validation to check for "duplicate emails"

class User < ApplicationRecord
    validates :email, {uniqueness: true}
end

Validation to check the number of characters

class Post < ApplicationRecord
    validates :content, {presence: true, length: {maximum: 140}}
end

Add column

id name(string) email(string)
1 alice [email protected]
2 tom [email protected]

Add user image information to this!

$ rails g migration add_image_name_to_users

Files are added to the migration folder / db / migration /.

How to read the migration file

Contents of /db/migration/20201010_create_post.rb

The change method was automatically generated in the migration file created by rails g model. So I was able to reflect the changes with rails db: migrate without any particular changes.

class CreateUsers < ActiveRecord::Migration[5.0]
    def change
        create_table :users do |t| #Create a table named posts
            t.string :name #Create a column named name whose data is string
            t.string :email #Create a column named email with data string
            t.timestamps
        end
    end
end

You have to add the columns yourself

This time, we will add a column, so we need to write the contents of the change method by ourselves.

class AddImageNameToUsers < ActiveRecord::Migration[5.0]
    def change
        add_column :uses, :image_name, :string #Table name, column name, data type
    end
end

Execute the contents of the change method

$ rails db:migrate

Save image

Writing a file

Use File.write.

$ rails console
> File.write("public/aaa.txt", "Hello World")
# File.write(Where to write,Contents)

Writing image data

Use the File.bin write and read methods. Write the read ʻimage to public / user_images / "1.jpg " `and save it.

    if image = params[:image]
      @user.image_name = "#{@user.id}.jpg "
      File.binwrite("public/user_images/#{@user.image_name}", image.read)
    end

Recommended Posts

Add, save, delete, add columns, save images from rails DB creation
Add & save from rails database creation
[Rails] Save images using carrierwave
[Ruby on Rails] Add / Remove Columns
Add, replace, delete Java PDF images
[Rails] Yeah! Active Strage! Competent Gem! ~ You can save images from two tables ~
How to add / remove Ruby on Rails columns