Ruby on rails 在RubyonRails上创建公告板

Ruby on rails 在RubyonRails上创建公告板,ruby-on-rails,ruby,cancancan,enumerize,Ruby On Rails,Ruby,Cancancan,Enumerize,有必要写一个按类别划分广告的公告板(广告可以同时在不同的类别中)。 广告必须有多个状态(审核/批准/拒绝),只有管理员才能更改状态 告诉我需要在模型中为广告表和类别创建哪些字段?我如何把它们绑在一起 提前感谢您的帮助。StackOverflow是一个英文网站;请尽量避免使用俄语!(不过,我把你的评论转给了一位翻译……)对不起,汤姆。谢谢)声明表能否创建一个字段:category\u id,以及category字段:advert\u id?我知道有必要创建API?根据需要,create_joins

有必要写一个按类别划分广告的公告板(广告可以同时在不同的类别中)。 广告必须有多个状态(审核/批准/拒绝),只有管理员才能更改状态

告诉我需要在模型中为广告表和类别创建哪些字段?我如何把它们绑在一起


提前感谢您的帮助。

StackOverflow是一个英文网站;请尽量避免使用俄语!(不过,我把你的评论转给了一位翻译……)对不起,汤姆。谢谢)声明表能否创建一个字段:category\u id,以及category字段:advert\u id?我知道有必要创建API?根据需要,
create_joins_表
将在JOIN表中创建
category_id
advert_id
列。因为它是一个**拥有且**属于多个关联,所以在
广告
表中没有
类别id
字段。谢谢,汤姆)我将尝试实现它。至于API。。。我不知道你想建什么。你的问题没有说。但很可能是的,您需要某种API。这将包括
routes.rb
、控制器操作以及可能的视图/序列化程序,具体取决于您试图实现的目标。我建议您通过rails教程开始:)
# 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
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