Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 778087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:42:34+00:00 2026-05-14T19:42:34+00:00

I’m just diving into Datamapper (and Sinatra) and have a question about associations. Below

  • 0

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
    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:42:35+00:00Added an answer on May 14, 2026 at 7:42 pm

    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"])

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I want use html5's new tag to play a wav file (currently only supported
I have a JSP page retrieving data and when single or double quotes are
I'm looking for suggestions for debugging... If you view this site in Firefox or
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.