Having the following associations between 3 models:
workout.rb
class Workout < ActiveRecord::Base
has_and_belongs_to_many :workout_sets, :join_table => :workout_sessions
belongs_to :warmup, :class_name => :WorkoutStep, :foreign_key => "workout_step_id"
accepts_nested_attributes_for :workout_sets, allow_destroy: true
accepts_nested_attributes_for :warmup, allow_destroy: true
end
workout_set.rb
class WorkoutSet < ActiveRecord::Base
has_and_belongs_to_many :workout_steps, :join_table => :sets_steps, dependent: :destroy
has_and_belongs_to_many :workouts, :join_table => :workout_sessions
accepts_nested_attributes_for :workout_steps, allow_destroy: true
has_one :intro_video_usage, class_name: 'VideoUsage::Intro', as: :parent, dependent: :destroy
has_one :intro_video, through: :intro_video_usage, source: :video
accepts_nested_attributes_for :intro_video
has_one :get_ready_video_usage, class_name: 'VideoUsage::GetReady', as: :parent, dependent: :destroy
has_one :get_ready_video, through: :get_ready_video_usage, source: :video
has_one :congrats_video_usage, class_name: 'VideoUsage::Congratulations', as: :parent, dependent: :destroy
has_one :congrats_video, through: :congrats_video_usage, source: :video
end
and
workout_step.rb
class WorkoutStep < ActiveRecord::Base
has_and_belongs_to_many :workout_sets, :join_table => :sets_steps
has_many :main_video_usage, class_name: 'VideoUsage::Main', as: :parent
has_many :main_videos, through: :main_video_usage, source: :video
accepts_nested_attributes_for :main_videos
end
And using simple_form and cocoon to handle nested models creation on the top level model (Workout
) I'm having troubles building the form for sets
and steps
- more concise, when associating a workout_set
with an intro_video
(and whitelisting the params) I'm having the following error:
Video(#70285207226600) expected, got String(#70285080848240)
The params
object after sending looks like this:
"workout"=>{"title"=>"",
"workout_sets_attributes"=>{"0"=>{"_destroy"=>"false",
"intro_video"=>"70",
"title"=>""}}},
"image"=>"",
"sound_logo"=>"",
"intro_video"=>"",
"commit"=>"Create workout"}
Thanks in advance.
Your parameters are passing a string ("70") to intro_video=
but association accessors like that expect you to pass an actual instance of the associated class (in this case Video).
You should instead be assigning to intro_video_id
. The accessor will convert the string to an integer for you.