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

  • SEARCH
  • Home
  • 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 4273044
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:39:35+00:00 2026-05-21T07:39:35+00:00

That title probably doesn’t sound right, but forgive me, I’m learning Rails for the

  • 0

That title probably doesn’t sound right, but forgive me, I’m learning Rails for the first time after ten years of windows development. Currently, I have a model named Drill that should contain two exercises. To accomplish this based on my current knowledge of Rails, I created a has_many association between the Drill and Exercise models…

class Drill < ActiveRecord::Base
  has_many :exercises, :dependent => :destroy
end

But I don’t want an open ended list of exercises, instead I’d like to have two references to a single Exercise object, one called left_drill and the other called right_drill. Based on the documentation, I’ve changed the code to this…

class Drill < ActiveRecord::Base
  has_one  :right_drill, :class => :exercise
  has_one  :left_drill, :class => :exercise
end

But I don’t know if that’s right and I’m having a hard time testing it because I don’t know how to adjust the Exercise model respectively. As of now, the Exercise model remains the same…

class Exercise < ActiveRecord::Base
  belongs_to :drill
end

Having a hard time figuring out how to accomplish this and, while I continue to read through the docs, I was hoping someone could give me a little direction.

Thanks so much for your wisdom!

  • 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-21T07:39:35+00:00Added an answer on May 21, 2026 at 7:39 am

    What you have is correct, though typically the explicit class name is given as a string like so:

    class Drill < ActiveRecord::Base
      has_one :left_drill, :class => "Exercise"
      has_one :right_drill, :class => "Exercise"
    end
    

    You can also easily access both drill objects like so, where my_drill is an instance of Drill:

    my_drill.left_drill
    my_drill.right_drill
    

    You may wish to consider renaming them left_exercise and right_exercise to better reflect that they are an instance of the Exercise model, not the Drill model.

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

Sidebar

Related Questions

Sorry, that title probably doesn't make much sense, but what I want to know
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
That was probably a very badly worded question, but I'm curious how Rails knows
That title may need some work. Perhaps after reading this, you may be able
OK, that title is a little unclear, but I can't think of a better
First of all, sorry about that title. I'm not the best at writing those
(I realize that my title is poor. If after reading the question you have
I know that the title doesn't really say what I'm actually looking for, as
Alright, probably not the best title, but meh. I have the following code inside
I think the title for this question is probably wrong, but I'm not sure

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.