[Ruby] Create an EC site with Rails5 ③-Model association and other settings-

2 minute read

Introduction

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

Source code

https://github.com/Sn16799/bakeryFUMIZUKI

Model part setting

Set up associations and validations.

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 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

Now you should be ready. I would like to start implementing from the next time. It has nothing to do with the core of the function, but you have to decide the color scheme and design of the site. At the time of the practice of team implementation, I was talking about “let’s use a Japanese color scheme” at the beginning, but when I noticed it was nothing but Japanese, so why not try to think about Japanese style design this time? I am in the process of planning.

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

Recommended color sample site

https://www.color-sample.com/ A site that collects color samples for people in the Heian era, combining the colors of kimono and wearing them, called “Kasane’s eyes”. Although there are some similar sites, it is convenient because they have color codes.

https://colordrop.io/ You can see endlessly the color samples that are composed of four colors. It is possible to search by a specific color, or add and like to see them all together. **It’s been 2 hours since I noticed. **

https://www.schemecolor.com/ Many combinations of 2 to 6 colors are listed. Enhanced search and sort functions. **It’s been 2 hours since I noticed. **