<% 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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! > > DannyIn your controller: @unique_rooms = @room.uniq In your view: <% for room in @unique_rooms %> <option value="<%= room.city %>"><%= room.city %></option> <% end %> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > 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 %> -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 16, 2007, at 12:11 PM, 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 %><% 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org Skype: rob.biedenharn --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> (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 -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---