[RUBY] [Rails] Implementation of many-to-many category functions

Target

ezgif.com-video-to-gif (1).gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3Implementation of posting functionCategory function implementation

Implementation

1. Create an intermediate table

Terminal


$ rails g model BookCategory book_id:integer category_id:integer

Terminal


$ rails db:migrate

schema.rb


create_table "book_categories", force: :cascade do |t|
  t.integer "book_id"
  t.integer "category_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

2. Delete column

Terminal


$ rails g migration RemoveCategoryIdFromBooks category_id:integer

~_remove_category_id_from_books


class RemoveCategoryIdFromBooks < ActiveRecord::Migration[5.2]
  def change
    remove_column :books, :category_id, :integer
  end
end

Terminal


$ rails db:migrate

3. Edit each model

book.rb


has_many :book_categories
has_many :categories, through: :book_categories

category.rb


has_many :book_categories
has_many :books, through: :book_categories

book_category.rb


#Postscript
belongs_to :book
belongs_to :category

4. Edit the controller

Make category_id arrayable with the strong parameter of books_controller.rb.

books_controller.rb


def book_params
  params.require(:book).permit(:title, :body, { category_ids: [] })
end

5. Edit the view

** ① Edit the form **

slim:books/index.html.slim


/Change before
= f.label :category_id, 'Category'
br
= f.collection_select :category_id, Category.all, :id, :name, { prompt: 'Please select' }, class: 'form-control'
br

/After change
= label_tag 'Category'
br
= collection_check_boxes(:book, :category_ids, Category.all, :id, :name) do |cb|
  = cb.label { cb.check_box + ' ' + cb.text }
br

= collection_check_boxes(:book, :category_ids, Category.all, :id, :name) do |cb| ➡︎ The names of all categories are displayed as checkboxes and the value is set to id.

** ② Edit the table **

slim:books/index.html.slim


/Change before
td
  = category.name

/After change
td
  - book.categories.each do |category|
    = category.name
    | /

Recommended Posts

[Rails] Implementation of many-to-many category functions
[Rails] Implementation of category function
Rails implementation of ajax removal
[Rails 6] Implementation of search function
Implementation of category pull-down function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Implementation of multi-layer category function using ancestry "Editing form"
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] gem ancestry category function implementation
[Rails] Types of associations (one-to-many / many-to-many)
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Many-to-many creation
Implementation of GKAccessPoint
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of "notify notification in some way"
[Rails] Implementation of batch processing using whenever (gem)
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Ruby on Rails <2021> Implementation of simple login function (form_with)
[rails] gem'payjp' implementation procedure
Implementation of flash messages
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Rails] Introduction of PAY.JP
Rails Action Text implementation
Implementation of search function
[Ruby on Rails] Implementation of tagging function/tag filtering function
Rails Tutorial/Significance of Indexing
Rails search function implementation
Implementation of pagination function
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
Ruby on Rails for beginners! !! Summary of new posting functions
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] How to display the list of posts by category
[Swift] Simple implementation of UIImageView
Rails fuzzy search function implementation
[Rails] Introduction of devise Basics
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
Implementation of sequential search function