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!
What you have is correct, though typically the explicit class name is given as a string like so:
You can also easily access both drill objects like so, where
my_drillis an instance ofDrill:You may wish to consider renaming them
left_exerciseandright_exerciseto better reflect that they are an instance of theExercisemodel, not theDrillmodel.