【发布时间】:2013-07-10 01:34:49
【问题描述】:
我正在努力解决我的关系,但我无法使用这些关联。
所以我有三个模型Workout、Exercise 和WorkoutExercise。一个锻炼应该有很多锻炼,一个锻炼应该有不同的锻炼,因此我写道:
class Workout < ActiveRecord::Base
has_many :workout_exercises
has_many :exercises, :through => :workout_exercises
end
class Exercise < ActiveRecord::Base
has_many :workout_exercises
has_many :workouts, :through => :workout_exercises
end
class WorkoutExercise < ActiveRecord::Base
belongs_to :exercise
belongs_to :workout
end
我正在运行一些测试,但是一旦我创建了一个锻炼、锻炼然后将它们加入到锻炼_锻炼类中,测试就没有通过。它不会让我像这样访问锻炼中的练习:
Workout.create
Exercise.create
WorkoutExercise.create(:workout => Workout.first, :exercise => Exercise.first)
work = Workout.first
work.exercises.count #This line causes the error: undefined method exercises
我的数据库表如下所示:
class CreateWorkouts < ActiveRecord::Migration
def change
create_table :workouts do |t|
t.string :title
t.text :description
t.float :score
t.timestamps
end
end
end
class CreateExercises < ActiveRecord::Migration
def change
create_table :exercises do |t|
t.string :title
t.text :description
t.float :value
t.timestamps
end
end
end
class CreateWorkoutExercises < ActiveRecord::Migration
def change
create_table :workout_exercises do |t|
t.timestamps
end
end
end
当我运行这个测试时,它说exercises 是未定义的。有没有人有任何想法?
【问题讨论】:
-
您运行迁移了吗?请向我们展示您的 3 张桌子。而且我认为你应该暂时忽略彦浩的建议。您的代码似乎是正确的,因此您现在不必更改它。您还缺少其他东西。
-
@Ashitaka 我添加了上面的表格,是否与 CreateWorkoutExercises 表格为空有关?这是我第一次使用 habtm。
-
好的,就是这样。您缺少在两个表之间建立连接的 id。您现在可能想要重新创建迁移。我认为
rake db:reset会完成这项工作(不过它会删除你的所有记录)。
标签: ruby-on-rails ruby