Не удалось найти MetricLocationHelper с идентификатором = 62 для Metric :: MetricDirector с идентификатором = 2
У меня есть has_and_belongs_to_many
ассоциация, и мои модели
class MetricLocationHelper < ActiveRecord::Base
has_and_belongs_to_many :metric_metric_directors, class_name: "Metric::MetricDirector", association_foreign_key: "metric_metric_director_id"
end
и
class Metric::MetricDirector < ActiveRecord::Base
has_and_belongs_to_many :metric_location_helpers, foreign_key: 'metric_metric_director_id'
accepts_nested_attributes_for :metric_location_helpers, reject_if: :all_blank, allow_destroy: true
end
Мои третьи столбцы таблицы:
- metric_metric_director_id
- metric_location_helper_id
А в контроллере
def metric_director_params
params.require(:metric_metric_director).permit(:name, :username, :region, metric_location_helpers_attributes: [:id, :_destroy])
end
У меня возникла проблема при обновлении и создании записи.
ActiveRecord :: RecordNotНайдено в метрике :: MetricDirectorsController # update
Не удалось найти MetricLocationHelper с ID = 62 для Metric :: MetricDirector с ID = 2
Обновлен код
class Metric::MetricDirectorsController < ApplicationController
before_action :set_metric_director, only: [:show, :edit, :update, :destroy]
def new
@metric_director = Metric::MetricDirector.new
@locations = @metric_director.metric_location_helpers.build
end
def edit
@locations = @metric_director.metric_location_helpers
end
def create
@metric_director = Metric::MetricDirector.new(metric_director_params)
if @metric_director.save
redirect_to metric_metric_directors_path, notice: 'Director was successfully created.'
else
render :new
end
end
def update
if @metric_director.update(metric_director_params)
redirect_to metric_metric_directors_path, notice: 'Director was successfully updated.'
else
render :edit
end
end
private
def set_metric_director
@metric_director = Metric::MetricDirector.find(params[:id])
end
def metric_director_params
params.require(:metric_metric_director).permit(:name, :username, :region, metric_location_helpers_attributes: [:id, :_destroy])
end
Ваша помощь очень ценится
Спасибо