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 == How do I get a uniq physical exercice listing? Actually : * Athlétisme (100m) * Badminton * Boxe * Boxe * Conditionnement physique * Conditionnement physique to : * Athlétisme (100m) * Badminton * Boxe * Conditionnement physique I use Rails 1.2.6 Thx a lot for your time and help. Hugues -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I manage to get it the way I wanted using : @personal_trainers_physical_exercices = PersonalTrainer.find(:all, :select => ''DISTINCT physical_exercice_id'') -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hugues Brunelle wrote:> I manage to get it the way I wanted using : > > @personal_trainers_physical_exercices = PersonalTrainer.find(:all, > :select => ''DISTINCT physical_exercice_id'')Why not PhysicalExercice.find :all ? BTW, note that the English spelling is "exercise". Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. --001485f85a0c112230047c6d7825 Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --001485f85a0c112230047c6d7825--
Hugues Brunelle wrote:> 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 > endI 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. personal_trainer = PersonalTrainer.first personal_trainer.physical_exercices * Athlétisme (100m) * Badminton * Boxe * Conditionnement physique 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. -- Posted via http://www.ruby-forum.com/. --00032555412a90f87c047c6e06cd Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --00032555412a90f87c047c6e06cd--
> Why not PhysicalExercice.find :all ?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. Hugues -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote:> Hugues Brunelle wrote: >> 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. > > personal_trainer = PersonalTrainer.first > personal_trainer.physical_exercices > * Athlétisme (100m) > * Badminton > * Boxe > * Conditionnement physique > > 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. Soon I will try what you suggest. Thanks again and I might write to you again. Regards, Hugues -- Posted via http://www.ruby-forum.com/. --0015174c0d5c4b8616047c804e12 Content-Type: text/plain; charset=ISO-8859-1 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. --0015174c0d5c4b8616047c804e12--
Hugues Brunelle wrote:>> Why not PhysicalExercice.find :all ? > > 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" -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> PhysicalExercice.find :all, :conditions => "personal_trainer_id IS NOT NULL"Actually I do not have a personal_trainer_id but as soon as I make an upgrade for the many-to-many association, I''ll be able to try it. Thx a lot for your help :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.