# Select distinct records from an Array

**URL:** <https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929>\
**Category:** Rails\
**Created:** [December 31, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929 "2009-12-31T20:31:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [December 31, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/1 "2009-12-31T20:31:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 5, 2010, 4:08pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/2 "2010-01-05T16:08:54Z")

</div>

I manage to get it the way I wanted using :

@personal\_trainers\_physical\_exercices = PersonalTrainer.find(:all,  
:select =\> ‘DISTINCT physical\_exercice\_id’)

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 5, 2010, 5:38pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/3 "2010-01-05T17:38:33Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 5, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/4 "2010-01-05T18:18:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 6, 2010, 3:57pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/5 "2010-01-06T15:57:11Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 6, 2010, 4:06pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/6 "2010-01-06T16:06:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 6, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/7 "2010-01-06T17:54:19Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [January 6, 2010, 9:04pm UTC](https://www.ruby-forum.com/t/select-distinct-records-from-an-array/180929/8 "2010-01-06T21:04:05Z")

</div>

> 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 🙂
