Table design (using Active Hash)

Overview

This time, I designed the table to create a clone site for flea market apps. Since I made it myself for the first time, the quality of the table design is still unclear, but for the time being, please overlook it as an output ...

ER diagram

It became like this. テーブル設計.png

The point is to prepare a file using Active Hash for the part to be selected from the existing data. The condition of the product, the prefecture of the delivery destination at the time of purchase, etc. Regarding the prefectures (active_hash) table that represents prefectures, we use two locations, the shipping source and the destination.

How to use Active Hash

Installation

Put the following in the Gemfile and bundle install.

Gemfile


gem 'active_hash'

Now create a model for Active Hash. ⚠︎ Create your own model that inherits ActiveHash :: Base instead of creating it with commands in the terminal ⚠︎

File description

As an example, let's take a category file.

app/models/category.rb


class Category < ActiveHash::Base
  self.data = [
    {id: 1, name: '---'},{id: 2, name: 'Women'},{id: 3, name: 'mens'},{id: 4, name: 'Baby / Kids'},{id: 5, name: 'Interior / house / accessories'},{id: 6, name: 'Books / music / games'},{id: 7, name: 'Toys, hobbies and goods'},{id: 8, name: 'Home appliances, smartphones, cameras'},{id: 9, name: 'sport, leisure'},{id: 10, name: 'Handmade'},{id: 11, name: 'Other'}
  ]
end

README

In addition, the README of the table and association designed this time is as follows.

README.md


#Table design

##users table
| Column      | Type   | Options     |
| ----------- | ------ | ----------- |
| nickname    | string | null: false |
| email       | string | null: false |
| password    | string | null: false |
| first_name  | string | null: false |
| family_name | string | null: false |
| read_first  | string | null: false |
| read_family | string | null: false |
| birth       | date   | null: false |

### Association

- has_many :products
- has_many :item_purchases
- has_many :comments

##products table
| Column              | Type       | Options     |
| ------------------- | ---------- | ----------- |
| photo               | text       | null: false |
| name                | string     | null: false |
| explanation         | text       | null: false |
| category            | integer    | null: false |
| condition           | integer    | null: false |
| postage_type        | integer    | null: false |
| prefectures         | integer    | null: false |
| preparation_days    | integer    | null: false |
| value               | integar    | null: false |
| user                | references | null: false | 


### Association

- belongs_to :user
- has_one :item_purchase
- has_many :comments
- belongs_to_active_hash :category
- belongs_to_active_hash :condition
- belongs_to_active_hash :postage_type
- belongs_to_active_hash :prefectures
- belongs_to_active_hash :preparation_days
- belongs_to :seller, class_name: "User"


## item_purchases table
| Column        | Type    | Options                        |
| ------------- | ------- | ------------------------------ |
| product       | integer | null: false, foreign_key: true |
| user          | integer | null: false, foreign_key: true |
| purchase_info | integer | null: false, foreign_key: true |

### Association

- belongs_to :user
- belongs_to :product
- belongs_to :purchase_info


##comments table
| Column  | Type       | Options                        |
| ------- | ---------- | ------------------------------ |
| content | string     | null: false                    |
| user    | integer    | null: false, foreign_key: true |
| product | integer    | null: false, foreign_key: true |

### Association

- belongs_to :product
- belongs_to :user

## purchase_info table

| Column        | Type       | Options                        |
| ------------- | ---------- | ------------------------------ |
| postal_code   | string     | null: false                    |
| prefectures   | integer    | null: false, foreign_key: true |
| city          | string     | null: false                    |
| address       | string     | null: false                    |
| building_name | string     |                                |
| phone_number  | string     | null: false                    |
| item_purchase | integer    | null: false, foreign_key: true |

### Association

- has_one_active_hash :prefectures
- has_one :item_purchase

Note the dependency between has_one and belongs_to here.

If you need a foreign key, you need to have an association with belongs_to.

Last but not least, the first self-implementation took a lot of time, but it was fun. However, it took about 10 hours just to do this today, so I will do my best not to be frustrated ...

Recommended Posts

Table design (using Active Hash)
[Rails] About active hash
[Rails] Introducing Active Hash
Utilization of Active Hash
Pitfalls of Active Hash
Category search with ransack using categories created with Active hash
gem active_hash About active hash
I tried to explain Active Hash