create join table

# console
rails g migration CreateJoinTablePostPostCategory post post_category

# migration
class CreateJoinTablePostPostCategory < ActiveRecord::Migration
  def change
    create_join_table :posts, :post_categories do |t|
      # t.index [:post_id, :post_category_id]
      # t.index [:post_category_id, :post_id]
    end
  end
end

# model
class Post < ActiveRecord::Base
  extend FriendlyId
  belongs_to :admin
  belongs_to :restaurant
  friendly_id :title, use: [:slugged, :scoped], scope: :restaurant
end

class PostCategory < ActiveRecord::Base
end

results matching ""

    No results matching ""