rails 模型 has_many :通过关联 [英] rails model has_many :through associations

查看:47
本文介绍了rails 模型 has_many :通过关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决我的人际关系,但在使用关联时遇到问题.

I am trying to get my relationships worked out but I am having trouble using the associations.

所以我有三个模型WorkoutExerciseWorkoutExercise.一次锻炼应该有很多练习,而一次锻炼应该有不同的锻炼,所以我写道:

So I have three models Workout, Exercise and WorkoutExercise. A workout should have many exercises and a exercise should have different workouts therefore I wrote:

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

我正在运行一些测试,但是一旦我创建了锻炼、锻炼,然后将它们加入到 training_exercise 课程中,这些测试就没有通过.它不会让我像这样访问锻炼中的练习:

I am running some tests but the tests aren't passing once I create a workout, exercise and then join them in the workout_exercise class. It won't let me access the exercises in the workout like this:

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

我的数据库表如下所示:

My database tables look like this:

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 未定义.有没有人有任何想法?

When I run this tests it says exercises is undefined. Does anyone have any ideas?

推荐答案

好的,所以您的 WorkoutExercises 表不能为空.它应该是这样的:

Ok, so your WorkoutExercises table can't be empty. This is how it should look:

class CreateWorkoutExercises < ActiveRecord::Migration
  def change
    create_table :WorkoutExercises do |t|
      t.integer :exercise_id, :null => false
      t.integer :workout_id, :null => false

      t.timestamps
    end

    # I only added theses indexes so theoretically your database queries are faster.
    # If you don't plan on having many records, you can leave these 2 lines out.
    add_index :WorkoutExercises, :exercise_id
    add_index :WorkoutExercises, :workout_id
  end
end

此外,您可以随意命名此表,它不一定是 WorkoutExercises.但是,如果您使用的是 has_and_belongs_to_many 关系,则您的表必须强制命名为 ExercisesWorkout.注意锻炼是如何在锻炼之前出现的.名称必须按字母顺序排列.不要问我为什么,这只是 Rails 约定.

Also, you can name this table whatever you'd like, it doesn't have to be WorkoutExercises. However, if you were using a has_and_belongs_to_many relationship, your table would have to mandatorily be named ExercisesWorkout. Notice how Exercises comes before Workout. The names have to be alphabetically ordered. Don't ask me why, it's just a Rails convention.

因此,在这种情况下,您可以将表格命名为 WorkoutExercises.但如果我是你,我会把它改成锻炼锻炼,以防万一,这样你就永远不会出错.

So, in this case, you'll do fine with your table being named WorkoutExercises. But if I were you, I'd change it to ExercisesWorkout, just in case, so that you never get it wrong.

这篇关于rails 模型 has_many :通过关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆