Hello there,
Project is about a personal trainers directory. I would like to create a
listing (filter) of available physical exercices to browse the directory
by physical exercice.
My Models are :
class PersonalTrainer < User
belongs_to :physical_exercice
end
class PhysicalExercice < ActiveRecord::Base
has_many :personal_trainers
end
Project is about a personal trainers directory. I would like to create a
listing (filter) of available physical exercices to browse the directory
by physical exercice.
class PersonalTrainer < User
belongs_to :physical_exercice
end
class PhysicalExercice < ActiveRecord::Base
has_many :personal_trainers
end
I have a feeling that what you’re really going to want here is a
many-to-many association rather than a one-to-many.
I get this feeling because of how your current associations read:
“Personal trainer belongs to a physical exercice.” While technically
accurate as far as Rails relationships go. I would not say that a person
belongs to an exercice. This makes me think these two are not so
directly associated.
So I would then consider this:
“A physical exercice may have many personal trainers. Each personal
trainer can instruct in many physical exercices through qualification.”
class PersonalTrainer < User
has_many :qualifications
has_many :physical_exercices, :through => :qualifications
end
class PhysicalExercice < ActiveRecord::Base
has_many :qualifications
has_many :personal_trainers, :through => :qualifications
end
class Qualification < ActiveRecord::Base
belongs_to :personal_trainer
belongs_to :physical_exercice
end
Now PhysicalExcercice.all will return a unique list of exercices
regardless of how many personal trainers are qualified to instruct in
the exercise. The new Qualification model will track which trainers are
qualified on which exercices.
Because some PhysicalExercise don’t have any PersonalTrainer offering it
as a main speciality. Since I want to filter by PhysicalExercise, I want
to be sure that it doen’t return a nil value.
BTW, note that the English spelling is “exercise”.
You’re right about Exercise, thanks for pointing that out. Will make the
correction.
Project is about a personal trainers directory. I would like to create a
listing (filter) of available physical exercices to browse the directory
by physical exercice.
class PersonalTrainer < User
belongs_to :physical_exercice
end
class PhysicalExercice < ActiveRecord::Base
has_many :personal_trainers
end
I have a feeling that what you’re really going to want here is a
many-to-many association rather than a one-to-many.
I get this feeling because of how your current associations read:
“Personal trainer belongs to a physical exercice.” While technically
accurate as far as Rails relationships go. I would not say that a person
belongs to an exercice. This makes me think these two are not so
directly associated.
So I would then consider this:
“A physical exercice may have many personal trainers. Each personal
trainer can instruct in many physical exercices through qualification.”
class PersonalTrainer < User
has_many :qualifications
has_many :physical_exercices, :through => :qualifications
end
class PhysicalExercice < ActiveRecord::Base
has_many :qualifications
has_many :personal_trainers, :through => :qualifications
end
class Qualification < ActiveRecord::Base
belongs_to :personal_trainer
belongs_to :physical_exercice
end
Now PhysicalExcercice.all will return a unique list of exercices
regardless of how many personal trainers are qualified to instruct in
the exercise. The new Qualification model will track which trainers are
qualified on which exercices.
physical_exercice = PhysicalExercice.first
physical_exercice.personal_trainers
* Bill
* Steve
* Frank
Note: I’m only guessing at your needs, but I have a feeling that this is
really what you’re intending.
First of all thank you for your time.
The many-to-many association is one task I need to do soon. The original
concept was to categorize each PersonalTrainer with ONE PhysicalExercise
to avoid PersonalTrainers to select all PhysicalExercise. I will make a
main (ONE) and a secondary (MANY) PhysicalExercise for PersonalTrainer.
Because some PhysicalExercise don’t have any PersonalTrainer offering it
as a main speciality. Since I want to filter by PhysicalExercise, I want
to be sure that it doen’t return a nil value.
PhysicalExercice.find :all, :conditions => “personal_trainer_id IS NOT
NULL”