与DataMapper的开始,协会的问题 [英] Beginning with Datamapper, Association question

查看:149
本文介绍了与DataMapper的开始,协会的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是潜入DataMapper的(和西纳特拉),并有一个关于关联问题。下面是一些机型我有。这就是我想实现的东西。我在与Workoutitems和锻炼的问题。锻炼将单独管理,但Workoutitems与每一行相关联的单个锻炼。


  1. 锻炼 - 只是一个类型列表
    训练(跑,电梯,仰卧起坐等)

  2. 选择的锻炼 - 这
    是一组锻炼的名称,
    随着用户的笔记和
    教练。它具有N个集合
    workoutitems

  3. Workoutitems - 这需要一个锻炼和一些重复它,去锻炼集

 


类锻炼
      包括的DataMapper ::资源
      属性:ID,序列#PK ID
      属性:名称,字符串:长度=> 50:要求=>真#锻炼名称
      属性:描述,字符串:长度=> 255 #workout说明
结束
    类Selectedworkout
      包括的DataMapper ::资源
      属性:ID,序列
      属性:名称,字符串:长度=> 50:要求=>真
      属性:workout_time,字符串:长度=> 20
      属性:user_notes,字符串:长度=> 255
      属性:coach_notes,字符串:长度=> 255
      有n,:workoutitems
    结束    类Workoutitem
      包括的DataMapper ::资源
      属性:ID,序列
      房地产:销售代表,字符串:长度=> 50:要求=>真
      belongs_to的:selectedworkout
    结束


解决方案

我回答就在,我要指出的是典型的红宝石约定(这是相关的DataMapper你会在第二个看到的)是有类的名称,如SelectedWorkout和WorkoutItem,而不是Selectedworkout和Workoutitems。从类名的DataMapper名你的人际关系自动,所以它是非常有用遵循惯例。道歉,如果这是令人困惑,但对于答案的目的,我将假设你可以为你的模型:

由于你的锻炼模式在本质上是从WorkoutItem数据的标准化收集,我建议 WorkoutItem.belongs_to(:锻炼)(顺带这是一个命令你可以从IRB运行,它会工作得很好,或者你可以坚持的 belongs_to的:锻炼模型类课程)

好像SelectedWorkout是您的主界面变成你的数据,所以我你会做的事情好像是说presume @ user.selected_workouts.first.workout_items (用于第一选择的锻炼的项目)或类似物。

顺便说一句,你可以走的更远,并使用WorkoutItem作为锻炼和SelectedWorkout之间的连接模式,如果下面的关系成立:

WorkoutItem.belongs_to(:锻炼)

WorkoutItem.belongs_to(:selected_workout)

SelectedWorkout.has(无限,:workout_items)#N ==模型内无限

一旦previous关系建立你可以说:

SelectedWorkout.has(无限,:锻炼,:通过=>:workout_items)

同样,你设置的另一侧有许多通过关系类似:

Workout.has(无限,:workout_items)

Workout.has(无限,:selected_workouts,:通过=>:workout_items

现在,你可以不喜欢很酷的事情@ selected_workout.workouts.map {| W | w.name} 。或者,如果你想找到所有选定的训练,其中包括一个特殊的锻炼,你可以说 @ workout.selected_workouts

或者你也可以通过这样的DataMapper的查询语法做更多令人兴奋的事情

@workouts_that_dont_require_gear = SelectedWorkouts.all(workouts.name=>仰卧起坐,上拉,俯卧撑])

I'm just diving into Datamapper (and Sinatra) and have a question about associations. Below are some models I have. This is what I want to implemented. I'm having an issue with Workoutitems and Workout. Workout will be managed separately, but Workoutitems has a single workout associated with each row.

  1. Workout - just a list of types of workouts (run, lift, situps, etc)
  2. Selected workout - this is the name of a set of workouts, along with notes by the user and trainer. It has a collection of N workoutitems
  3. Workoutitems - this takes a workout and a number of repetitions to it that go in the workout set.

class Workout
      include DataMapper::Resource
      property :id,     Serial  #PK id
      property :name,   String, :length=>50,:required=>true  # workout name
      property :description, String, :length=>255  #workout description
end


    class Selectedworkout
      include DataMapper::Resource
      property :id,  Serial
      property :name, String, :length=>50, :required=>true
      property :workout_time, String, :length=>20
      property :user_notes, String, :length=>255
      property :coach_notes, String, :length=>255
      has n, :workoutitems
    end

    class Workoutitem
      include DataMapper::Resource
      property :id, Serial
      property :reps, String, :length=>50, :required=>true
      belongs_to :selectedworkout
    end

解决方案

Just before i answer, i'm going to point out that the typical ruby convention (which is relevant for DataMapper you'll see in a second) is to have class names like SelectedWorkout and WorkoutItem, rather than Selectedworkout and Workoutitems. DataMapper names your relationships automatically from class names, so it's useful to follow the convention. Apologies if it's confusing, but for the purpose of the answer, i'm going to assume you can rename your models:

Given that your Workout model is in essence a normalized collection of data from WorkoutItem, i'd suggest that WorkoutItem.belongs_to(:workout) (and incidentally that's a command you could run from IRB, and it'd work just fine, or you can stick the belongs_to :workout in the model class of course).

It seems like SelectedWorkout is your primary interface into your data, so i presume you will be doing things like saying @user.selected_workouts.first.workout_items (for the first selected workout's items) or the like.

Incidentally, you can go further, and use WorkoutItem as a join model between Workout and SelectedWorkout, if the following relationships are set up:

WorkoutItem.belongs_to(:workout)

WorkoutItem.belongs_to(:selected_workout)

SelectedWorkout.has(Infinity, :workout_items) # n == Infinity inside a Model

Once the previous relationship is set up you can say:

SelectedWorkout.has(Infinity, :workouts, :through => :workout_items)

Likewise you set up the other side of the has many through relationship similarly:

Workout.has(Infinity, :workout_items)

Workout.has(Infinity, :selected_workouts, :through => :workout_items

Now, you can do cool things like @selected_workout.workouts.map{ |w| w.name }. Or if you want to find all the selected workouts that include a particular workout you could say @workout.selected_workouts.

Or you can do more exciting things through DataMapper's query syntax like this:

@workouts_that_dont_require_gear = SelectedWorkouts.all("workouts.name" => ["Situps", "Pullups", "Pushups"])

这篇关于与DataMapper的开始,协会的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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