Я получаю эту ошибку
ActiveRecord :: RecordNotFound в CategoriesController # create Не удалось найти связь с 'id' = [4]
Моя схема:
create_table "relationships", force: :cascade do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["followed_id"], name: "index_relationships_on_followed_id"
t.index ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
t.index ["follower_id"], name: "index_relationships_on_follower_id"
end
create_table "categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.boolean "display_in_navbar", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Форма Мои категории
<%= f.input :name %>
<%= f.input :description %>
<%= f.association :active_relationships, collection: Category.order(:name), prompt: "Choose a Category" %>
Модель категории:
class Category < ApplicationRecord
has_and_belongs_to_many :posts
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
inverse_of: :follower,
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
inverse_of: :followed,
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
end
Модель отношений:
class Relationship < ApplicationRecord
belongs_to :follower, class_name: "Category"
belongs_to :followed, class_name: "Category"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
Контроллер категорий:
class CategoriesController < InheritedResources::Base
private
def category_params
params.require(:category).permit(:name, :description, :display_in_navbar, post_ids: [], active_relationship_ids: [])
end
end