设置 Rails has-many-through 关联

Setting up Rails has-many-through association

我有四个模型:

我正在尝试在模型之间建立关联,这样我就可以制作 ORM 语句,例如:

User.first.exercises.first.barbell_push

我想得到的是每个exercise for User,然后得到一个barbell_push结果。

以下是我目前的模型和关联:


UsersExercise

Table:

create_table :users_exercises do |t|
  t.integer :user_id
  t.integer :exercise_id
  t.date :date
  t.timestamps null: false
end

型号:

class UsersExercise < ActiveRecord::Base
  belongs_to :user
  belongs_to :exercise
end

Exercise

Table:

create_table :exercises do |t|
  t.integer :dead_lift_id
  t.integer :barbel_push_id
  t.integer :dumbbels_curl_id
  t.timestamps null: false
end

型号:

class Exercise < ActiveRecord::Base
  has_many :dead_lift, class_name: 'Result', foreign_key: 'exercise_id'
  has_many :barbel_push, class_name: 'Result', foreign_key: 'exercise_id'
  has_many :dumbbell_curl, class_name: 'Result', foreign_key: 'exercise_id'
  has_many :users_exercises, dependent: :destroy
  has_many :users, through: :users_exercises
end

Result

Table:

create_table :results do |t|
  t.integer :reps
  t.integer :weight
  t.integer :exercise_id
  t.timestamps null: false
end

型号:

class Result < ActiveRecord::Base
  belongs_to :exercise
end

User

型号:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable, :rememberable,
         :trackable, :validatable, :confirmable, :omniauthable, :omniauth_providers => [:google_oauth2]
  ROLES = [:admin, :user, :trainer, :dietician]
  has_many :sent_messages, :class_name => "Message", :foreign_key => "sender_id"
  has_many :received_messages, :class_name => "Message", :foreign_key => "receiver_id"
  has_many :users_exercises
  has_many :exercise, through: :users_exercises
end

我认为您只需要一个 table(您已经有 :users)

create_table :exercises do |t|
  t.integer :user_id
  t.string :name
  t.integer :weight
  t.integer :reps
  t.date :check_in_time
  t.date :check_out_time
  t.timestamps null: false
 end

"name" 列将是 barbell_push、dumbell_curl 等,从您表单中的集合字段中获取。从一项练习转到另一项练习时,这将需要 check_in 和 check_out,但这应该非常简单。有了这个你确实可以获得:

@user.barbell_push.date
@user.barbell_push.duration (time elapsed from check-in to check-out)
@user.barbell_push.reps
@user.barbell_push.weight
@user.dumbell_curl.reps
etc in whatever sequence you want to present the data.