选择我有一个Workout
模型:红宝石没有右表
class Workout < ActiveRecord::Base
has_one :exercise
belongs_to :session
has_many :exercise_equipment
end
和Exercise
模型
class Exercise < ActiveRecord::Base
has_many :workouts
end
,并在我的show
我有@workout.exercise.name
它抛出一个SQLException:
no such column: exercises.exercise_id: SELECT "exercises".* FROM "exercises" WHERE "exercises"."exercise_id" = ? LIMIT 1
尽管读了一些si milar的帖子,我真的不明白,为什么似乎SELECT exercises FROM exercises
这个has_one :exercise
行的结果并没有任何意义,我会假设,因为我使用的是Workout
对象,它会检查在workouts
表我
我试过
has_one :exercise, :class_name => 'Workout', :foreign_key => 'exercise_id'
但后来我得到
add_foreign_key :workouts, :exercises
add_foreign_key :exercises, :workouts
没有运气,巴克。
所以有人可以向我解释一下Ruby/Rails认为它在做什么以及我如何避免这个令人困惑的问题?
表
exercises
id:integer
name:text
description:text
created_at:text
updated_at:text
workouts
id:integer
name:text
exercise_id:integer
session_id:integer
created_at:text
updated_at:text
如果您在锻炼中有has_one或has_many,则必须在所引用的关联上有workout_id。关联模型将具有“belongs_to:锻炼” – Swards
尽管锻炼独立于锻炼而存在。我应该可以进行“跳跃杰克”运动,而不必说出哪些人的训练参考了运动,对吧?所以我仍然不确定它为什么试图从练习表中选择exercise_id。 – Csteele5
一次锻炼是否有很多锻炼?还是只有一个? – Swards