Forum: Ruby on Rails unique values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d79da034ee21c2aa1304e3c02e993eb?d=identicon&s=25 Nobody Else (nyxx)
on 2007-01-16 17:49
<% for room in @rooms %>
  <option value="<%= room.city %>"><%= room.city %></option>        <%
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
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-01-16 18:10
Daniel Owen van Dommelen wrote:
> <% for room in @rooms %>
>   <option value="<%= room.city %>"><%= room.city %></option>        <%
> 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 %>
   <option value="<%= room.city %>"><%= room.city %></option>
<% end %>
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-01-16 18:11
>
> In your controller:
>
> @unique_rooms = @room.uniq
>
> In your view:
>
> <% for room in @unique_rooms %>
>    <option value="<%= room.city %>"><%= room.city %></option>
> <% end %>

Er, on second thought why not just:

<% for room in @rooms.uniq %>
   <option value="<%= room.city %>"><%= room.city %></option>
<% end %>
9d79da034ee21c2aa1304e3c02e993eb?d=identicon&s=25 Nobody Else (nyxx)
on 2007-01-16 22:08
Drew Olson wrote:
>
>>
>> In your controller:
>>
>> @unique_rooms = @room.uniq
>>
>> In your view:
>>
>> <% for room in @unique_rooms %>
>>    <option value="<%= room.city %>"><%= room.city %></option>
>> <% end %>
>
> Er, on second thought why not just:
>
> <% for room in @rooms.uniq %>
>    <option value="<%= room.city %>"><%= room.city %></option>
> <% 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-01-16 22:34
(Received via mailing list)
On Jan 16, 2007, at 12:11 PM, Drew Olson wrote:
>   <option value="<%= room.city %>"><%= room.city %></option>
> <% end %>

<% for city in @rooms.map(&:city).uniq.sort %>
   <option value="<%= city %>"><%= city %></option>
<% end %>

or even: 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Skype:  rob.biedenharn
9d79da034ee21c2aa1304e3c02e993eb?d=identicon&s=25 Nobody Else (nyxx)
on 2007-01-17 10:26
> (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
This topic is locked and can not be replied to.