I'm working on an app but I'm having some trouble getting my head around how to structure my models. I've been in the front-end game for a long time now so I'm a little rusty with the back-end side of things.
First off, the problem, as summarised as I can get it.
This is a fitness app. There's a database of exercises, which holds attributes on the name of the exercise, instructions, video etc. For example.
class Exercise < ActiveRecord::Base attr_accessible :name, :description, :video_url end
The app will also have the ability to create a program, which will contain many exercises, while adding fields like weight and reps to the exercise.
This is where I'm a bit confused. From a database point of view, I can see myself creating a new table such as program_exercises, which contains weight and reps has a foreign key pointed to an entry in the exercises table and a foreign key pointed to the programs table. So, in rails, I could do something like this:
class Program < ActiveRecord::Base has_many :program_exercises end class Program_Exercise < ActiveRecord::Base attr_accessible :weight, :reps belongs_to :exercise belongs_to :program end
This should, in theory, work. However the terminology just doesn't seem correct, in real life a program_exercise doesn't really belong_to an exercise, it is an exercise, just with added data! I thought about inheritance, but that seems to be more in terms of inheriting methods/properties from a Class rather than inheriting actual instance data (plus, there'll be many program_exercises referencing back to a single exercise).
The problem is also compounded, because users should also be able to track their own progress for a particular exercise, which could lead to another model like so:
class User_Exercise < ActiveRecord::Base attr_accessible :completed_weight, :completed_reps belongs_to :user belongs_to :program_exercise end
Again, it works in theory, but now in Rails a user_exercise belongs to a program_exercise which belongs to an exercise. Which, I don't particularly like, because to get the exercise name on a user_exercise would be user_exercise.program_exercise.exercise.name. Considering that at it's core, a user_exercise is still just an exercise, I should just be able to go user_exercise.name instead.
I suppose I could do something like this, but to have to redefine things that exist seems weird.
class User_Exercise < ActiveRecord::Base ... def name program_exercise.exercise.name end end
Is there a better way to do this? Have I missed something, acts_as, or some type of instance inheritance? I'd appreciate any insight into this problem.
Thanks in advance.