-2

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.

Максим
  • 23
  • 1
  • 7

1 Answers1

0
# 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
Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • StackOverflow is an English website; please try to avoid Russian! (I put your comment through a translator, though...) – Tom Lord Jan 12 '18 at 13:12
  • Sorry, Tom. Thanks) Can the declaration table create a field: category_id, and for the Category field: advert_id? I understand it is necessary to create an API? – Максим Jan 12 '18 at 13:14
  • The `create_joins_table` will create the `category_id` and `advert_id` columns in the JOIN table as needed. Because it's a **has_and_**belongs_to_many association, there is no `category_id` field in the `adverts` table. – Tom Lord Jan 12 '18 at 13:14
  • Thanks, Tom)I'll try to implement it. – Максим Jan 12 '18 at 13:15
  • As for the API... I don't know what you want to build. Your question does not say. But most likely yes, you will need some sort of an API. This will consist of the `routes.rb`, controller actions, and possibly views/serializers -- depending on what you're trying to achieve. I suggest you go through a rails tutorial to get started :) – Tom Lord Jan 12 '18 at 13:16
  • Now I'll update the condition a bit so that it's clear. – Максим Jan 12 '18 at 13:31
  • Are you now asking how to build an entire website?! Is this a job interview question? Sorry, but this is *way* too broad for a StackOverflow post; you'll need to have a go at each part for yourself and ask more specific, narrow questions if stuck. – Tom Lord Jan 12 '18 at 14:41
  • I'm not asking you to help with the whole task! I wrote this because you did not understand the essence of the application!!!!I was slandered just in vain! What will others think now because of you. – Максим Jan 13 '18 at 05:24