# Unique values

**URL:** <https://www.ruby-forum.com/t/unique-values/81979>\
**Category:** Rails\
**Created:** [January 16, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/unique-values/81979 "2007-01-16T17:49:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 16, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/unique-values/81979/1 "2007-01-16T17:49:32Z")

</div>

\<% for room in @rooms %\>

 \<%= room.city %\> \<% end %\>

consider the above code… Is there an easy way in rails to get only the  
unique values, not the doubles? Since, for instance, Amsterdam (my city  
of habitat) will be returned about a thousend times or so, I want to  
make a list of cities that have rooms available.

I know how to code a function for this, I was just wondering about  
peoples thoughts on this (Am an absolute beginner myself, though been  
coding other languages for years)

Thanks in advance!

Danny

---

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 16, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/unique-values/81979/2 "2007-01-16T18:10:37Z")

</div>

Daniel Owen van Dommelen wrote:

> \<% for room in @rooms %\>
> 
> \<%= room.city %\> \<% end %\>
> 
> consider the above code… Is there an easy way in rails to get only the  
> unique values, not the doubles? Since, for instance, Amsterdam (my city  
> of habitat) will be returned about a thousend times or so, I want to  
> make a list of cities that have rooms available.
> 
> I know how to code a function for this, I was just wondering about  
> peoples thoughts on this (Am an absolute beginner myself, though been  
> coding other languages for years)
> 
> Thanks in advance!
> 
> Danny

In your controller:

@unique\_rooms = @room.uniq

In your view:

\<% for room in @unique\_rooms %\>

 \<%= room.city %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 16, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/unique-values/81979/3 "2007-01-16T18:11:52Z")

</div>

> In your controller:
> 
> @unique\_rooms = @room.uniq
> 
> In your view:
> 
> \<% for room in @unique\_rooms %\>
> 
> \<%= room.city %\> \<% end %\>

Er, on second thought why not just:

\<% for room in @rooms.uniq %\>

 \<%= room.city %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 16, 2007, 10:08pm UTC](https://www.ruby-forum.com/t/unique-values/81979/4 "2007-01-16T22:08:06Z")

</div>

Drew O. wrote:

> > In your controller:
> > 
> > @unique\_rooms = @room.uniq
> > 
> > In your view:
> > 
> > \<% for room in @unique\_rooms %\>
> > 
> > \<%= room.city %\> \<% end %\>
> 
> Er, on second thought why not just:
> 
> \<% for room in @rooms.uniq %\>
> 
> \<%= room.city %\> \<% end %\>

Thanks Drew, I believe that was the exact thing I was looking for,  
somehow I knew I would be this easy in ruby 😉

l8rs

---

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 17, 2007, 10:26am UTC](https://www.ruby-forum.com/t/unique-values/81979/5 "2007-01-17T10:26:24Z")

</div>

> (Of course, this begins to be a bad example as it doesn’t sound like  
> Room belongs\_to :city anyway.)

actually I believe that within my system (at work that is) rooms indeed  
doesn’t belong to city, BUT city belongs to rooms though… or to be  
true to form, city has many rooms 🙂

but thanks a lot anyway, in this beginners stage any info on any topic  
is most welcome!

- Danny

---

<div class="post-metadata">

**Author:** ![nyxx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/13edae/32.png) [@nyxx](https://www.ruby-forum.com/u/nyxx)\
**Post date:** [January 16, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/unique-values/81979/6 "2007-01-16T22:34:50Z")

</div>

On Jan 16, 2007, at 12:11 PM, Drew O. wrote:

> \<%= room.city %\> \<% end %\>

\<% for city in @rooms.map(&:city).uniq.sort %\>

 \<%= city %\> \<% end %\>

or even: [Parked at Loopia](http://rdoc.caboo.se/doc/classes/ActionView/Helpers/)  
FormOptionsHelper.html#M004193

\<%= options\_for\_select(@rooms.map(&:city).uniq.sort) %\>

If you needed the value to be the city\_id rather than the city  
(name), you could:

\<%= options\_for\_select(@rooms.map { |r| [ r.city,  
r.city\_id ] }.uniq.sort) %\>

(Of course, this begins to be a bad example as it doesn’t sound like  
Room belongs\_to :city anyway.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
Skype: rob.biedenharn
