Select distinct records from an Array

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

I manage to get it the way I wanted using :

@personal_trainers_physical_exercices = PersonalTrainer.find(:all,
:select => ‘DISTINCT physical_exercice_id’)

Hugues B. 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
[email protected]

Hugues B. 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.

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

Robert W. wrote:

Hugues B. 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

Hugues B. 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”

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 :slight_smile: