It is necessary to write a bulletin board with the division of ads by category (the ad can be in different categories at the same time). Ads must have several statuses (moderation / approved / rejected), only the administrator can change the status.
Tell me what fields in the model do I need to create for the ad table and categories? And how do I tie them together?
Thankful in advance for the help.
# app/models/advert.rb
class Advert < ApplicationRecord
has_and_belongs_to_many :categories
enum status: [:moderation, :approved, :rejected]
end
# app/models/category.rb
class Category < ApplicationRecord
has_and_belongs_to_many :adverts
end
You need to have a JOINS table, to link the two tables. By rails convention, this will be called adverts_categories
(but it's easy to override if preferred).
The database migrations to create these tables could, for example, be:
class CreateAdverts < ActiveRecord::Migration[5.0]
def change
create_table :adverts do |t|
t.integer :status
# ...
end
end
end
class CreateCategories < ActiveRecord::Migration[5.0]
def change
create_table :categories do |t|
# (What fields does this table have?)
t.string :name
# ...
end
end
end
class CreateAdvertsCategories < ActiveRecord::Migration[5.0]
def change
create_join_table :adverts, :categories do |t|
t.index :advert_id
t.index :category_id
end
end
end