Nested form - problem

Cześć wszystkim,
mam taką relację: Training has many Exercise through TrainingExercise i odwrotnie oczywiście. W modelu Exercise mam 7 scope’ów. Chciałbym mieć taki formularz, bym mógł wybrać sobie dzień treningu i zapisywać go do tabeli Training, a także wybrać sobie z siedmiu pól typu select (scope z modelu Exercise) jedno ćwiczenie z każdego scope’a i zapisać te dane do bazy TrainingExercise. Podsumowując, dane z formularza zapisywane byłyby tylko do tabel Training i TrainingExercise. Czy da się takie coś zrobić? Jeśli tak to proszę o podpowiedzi. THX

Da się. Pewnie nie o taką odpowiedź ci chodziło. Spróbuj napisać coś samemu, a jak będziesz miał z czymś problem to podziel się kodem i na konkretny problem ludzie na pewno Tobie pomogą.

Tak wyglądają moje kontrolery i modele w aplikacji.

class TrainingsController < ApplicationController

before_action :set_exercise, only: [:edit, :update, :destroy]

def index
@trainings = Training.all
end

def new
@training = Training.new
# @exercises = Exercise.all
@training_exercises = @training.training_exercises.build
end

def create
@training = Training.create(training_exercise_params)
redirect_to trainings_path
end

private

def training_exercise_params
  params.require(:training).permit(:date, training_exercises_attributes: [:id,:training_id, exercise: [:id]])
end

def set_exercise
  @exercise = Exercise.find(params[:id])
end

end

= simple_form_for @training do |f|
.form-inputs
= f.input :date
= f.simple_fields_for :training_exercises do |g|
= g.input :exercise, label: ‘Biceps’, collection: Exercise.biceps.pluck(:name_pl)
= g.input :exercise, label: ‘Triceps’, collection: Exercise.triceps.pluck(:name_pl)
= g.input :exercise, label: ‘Legs’, collection: Exercise.legs.pluck(:name_pl)
= g.input :exercise, label: ‘Stomach’, collection: Exercise.stomach.pluck(:name_pl)
= g.input :exercise, label: ‘Shoulders’, collection: Exercise.shoulders.pluck(:name_pl)
= g.input :exercise, label: ‘Back’, collection: Exercise.back.pluck(:name_pl)
= g.input :exercise, label: ‘Chest’, collection: Exercise.chest.pluck(:name_pl)
.form-actions
= f.button :submit, f.object.new_record? ? t(‘options.create’) : t(‘options.update’), class: ‘btn btn-success ban-block’

`class Exercise < ActiveRecord::Base
enum kind: [:biceps, :triceps, :stomach, :legs, :chest, :shoulders, :back]
has_many :training_exercises
has_many :trainings, through: :training_exercises

accepts_nested_attributes :trainings

TYPES = {biceps: 0, triceps: 1, stomach: 2, legs: 3, chest: 4, shoulders: 5, back: 6}.freeze
scope :biceps, -> { where(kind: TYPES[:biceps])}
scope :triceps, -> { where(kind: TYPES[:triceps])}
scope :chest, -> { where(kind: TYPES[:chest]) }
scope :back, -> { where(kind: TYPES[:back]) }
scope :shoulders, -> { where(kind: TYPES[:shoulders]) }
scope :stomach, -> { where(kind: TYPES[:stomach]) }
scope :legs, -> { where(kind: TYPES[:legs])}
end`

`class Training < ApplicationRecord
has_many :training_exercises, dependent: :destroy
has_many :exercises, through: :training_exercises

accepts_nested_attributes_for :exercises

accepts_nested_attributes_for :training_exercises
end`

class TrainingExercise < ApplicationRecord belongs_to :training belongs_to :exercise accepts_nested_attributes_for :exercise end