简体   繁体   English

Rails has_one关联预期模型得到字符串

[英]Rails has_one association expected Model got String

Having the following associations between 3 models: 在3个模型之间具有以下关联:

workout.rb training.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 training_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 training_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: 并使用simple_formcocoon在顶级模型( Workout )上处理嵌套模型的创建,我在构建setssteps的表单时遇到了麻烦-更简洁,当将workout_setintro_video (以及将参数列入白名单)相关联时,出现以下错误:

Video(#70285207226600) expected, got String(#70285080848240) 预期视频(#70285207226600),得到了字符串(#70285080848240)

The params object after sending looks like this: 发送后的params对象如下所示:

"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). 您的参数正在将字符串(“ 70”)传递给intro_video=但是类似的关联访问器希望您传递关联类的实际实例(在本例中为Video)。

You should instead be assigning to intro_video_id . 您应该改为分配给intro_video_id The accessor will convert the string to an integer for you. 访问器将为您将字符串转换为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM