问题
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.
- Workout - just a list of types of workouts (run, lift, situps, etc)
- 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
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
回答1:
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"])
来源:https://stackoverflow.com/questions/2826439/beginning-with-datamapper-association-question