Create an EC site with Rails5 ③-Set model associations and other things-

Introduction

This is a continuation of the series that creates an EC site where you can shop at a fictitious bakery, Create an EC site with Rails5②. This time is still in the preparation stage. We will set the relationship between Models.

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

Model part settings

Set the association and validation.

app/models/address.rb


belongs_to :customer

validates :post_code, presence: true, length: {minimum: 2, maximum: 10}
validates :addressee, presence: true, length: {minimum: 2, maximum: 35}
validates :address, presence: true, length: {minimum: 2, maximum: 50}

app/models/cart_item.rb


belongs_to :customer
belongs_to :product
validates :customer_id, presence: true
validates :product_id, presence: true

app/models/customer.rb


  has_many :cart_items
  has_many :products, through: :cart_items

  has_many :orders
  has_many :addresses

  validates :email, length: {minimum: 3, maximum: 80}
  validates :family_name, length: {minimum: 1, maximum: 15}
  validates :family_name_kana, length: {minimum: 1, maximum: 20}
  validates :first_name, length: {minimum: 1, maximum: 15}
  validates :first_name_kana, length: {minimum: 2, maximum: 20}
  validates :post_code, length: {minimum: 3, maximum: 10}
  validates :address, length: {minimum: 3, maximum: 50}
  validates :tel, length: {minimum: 3, maximum: 15}

app/models/genre.rb


has_many :products
validates :name, presence: true, length: {maximum:15,minimum:2}

app/models/order.rb


belongs_to :customer
has_many :order_items, dependent: :destroy
#Have multiple products via an intermediate table
has_many :products, :through => :order_items

belongs_to :customer

validates :addressee, presence: true, length: {maximum: 35, minimum: 2}
validates :post_code, presence: true, length: {maximum: 10, minimum: 2}
validates :send_to_address, presence: true, length: {maximum: 50, minimum: 3}

app/models/order_item.rb


belongs_to :order
belongs_to :product

app/models/product.rb


belongs_to :genre
has_many :cart_items
has_many :customers, through: :cart_items
has_many :order_items
has_many :products, through: :order_items
attachment :image
validates :name, presence: true, length: {maximum:30,minimum:2}
validates :introduction, presence: true, length: {maximum:250,minimum:2}
validates :price, presence: true

Postscript

You should be ready now. I would like to start implementing it next time. It has nothing to do with the core of the function, but it's time to decide on the color scheme and design of the site. At the time of the team implementation training, I was initially talking about "let's use a Japanese-style color scheme", but when I realized it, there was nothing in Japanese, so why not consider Japanese-style design this time? We are planning to do so.

Is it possible to express a Japanese taste with zero design knowledge? Continue to next time!

Recommended color sample site

https://www.color-sample.com/ A site called "Kasane's Color", a collection of color samples used by people in the Heian period to combine and dress kimono colors. There are similar sites as they are, but this one has a color code and is convenient.

https://colordrop.io/ You can see the color swatches that are combined in 4 colors endlessly. You can search by a specific color or like it and see it all together. ** It's been 2 hours before I noticed. ** **

https://www.schemecolor.com/ There are many combinations of 2 to 6 colors. Full search / sort function. ** It's been 2 hours before I noticed. ** **

Recommended Posts

Create an EC site with Rails5 ③-Set model associations and other things-
Create an EC site with Rails5 ⑤ ~ Customer model ~
Create an EC site with Rails5 ⑦ ~ Address, Genre model ~
Create an EC site with Rails 5 ⑩ ~ Create an order function ~
Create an EC site with Rails5 ⑥ ~ seed data input ~
Create an EC site with Rails5 ② ~ Bootstrap4 settings, Controller / action definition ~
Creating an EC site with Rails5 ①-App configuration, various gem preparation, Model / Routing creation-
Create an EC site using stripe! (Account creation)
Create a Service with an empty model Liferay 7.0 / DXP
[Rails] Create an application
Maybe it works! Create an image with Docker and share it!
Create Rails5 and postgresql environment with Docker and make pgadmin available
[Rails] EC site cart function
Create an app catalog site using CLI for Microsoft 365 with Docker