What is the best way to create a drop down where the viewable text in a concatenation of 2 or more columns? For instance, I hane a lookup table with these columns. Model FOO columns: id , name, phone In my drop select tag, I''d like the user to see: "name1 phone1" "name2 phone2" etc.. I know I can do this using find_by_sql . .. But, isn''t there a more elegant way? -- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060726/2b6f047e/attachment.html
> What is the best way to create a drop down where the viewable text in a > concatenation of 2 or more columns? > > For instance, I hane a lookup table with these columns. > Model FOO > columns: id , name, phone > > In my drop select tag, I''d like the user to see: > > "name1 phone1" > "name2 phone2" > etc.. > > I know I can do this using find_by_sql . .. > But, isn''t there a more elegant way?class FOO def name_plus_phone self.name + '' '' + self.phone end end @names = FOO.find(:all) <%= select_tag ''blah'', options_from_collection_for_select(@names ''id'', ''name_plus_phone'') %> -philip
On Tuesday, July 25, 2006, at 10:52 PM, Philip Hallstrom wrote:>> What is the best way to create a drop down where the viewable text in a >> concatenation of 2 or more columns? >> >> For instance, I hane a lookup table with these columns. >> Model FOO >> columns: id , name, phone >> >> In my drop select tag, I''d like the user to see: >> >> "name1 phone1" >> "name2 phone2" >> etc.. >> >> I know I can do this using find_by_sql . .. >> But, isn''t there a more elegant way? > >class FOO > def name_plus_phone > self.name + '' '' + self.phone > end >end > >@names = FOO.find(:all) > ><%= select_tag ''blah'', options_from_collection_for_select(@names >''id'', ''name_plus_phone'') %> > >-philip >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails<%= select ''model'', ''name'', @names.map {|x| ["#{x.name} - #{x.phone}", x.id]} %> This works too, and set''s the ''name_id'' attribute to the selected id. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
On 2006-07-26 00:05:32 -0400, Kevin Olbrich <devlists-rubyonrails@devlists.com> said:> > On Tuesday, July 25, 2006, at 10:52 PM, Philip Hallstrom wrote: >>> What is the best way to create a drop down where the viewable text in a >>> concatenation of 2 or more columns? >>> >>> For instance, I hane a lookup table with these columns. >>> Model FOO >>> columns: id , name, phone >>> >>> In my drop select tag, I''d like the user to see: >>> >>> "name1 phone1" >>> "name2 phone2" >>> etc.. >>> >>> I know I can do this using find_by_sql . .. >>> But, isn''t there a more elegant way? >> >> class FOO >> def name_plus_phone >> self.name + '' '' + self.phone >> end >> end >> >> @names = FOO.find(:all) >> >> <%= select_tag ''blah'', options_from_collection_for_select(@names >> ''id'', ''name_plus_phone'') %> >> >> -philip >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > <%= select ''model'', ''name'', @names.map {|x| ["#{x.name} - #{x.phone}", > x.id]} %> > > This works too, and set''s the ''name_id'' attribute to the selected id. > > _Kevin > www.sciwerks.comYou also can use composed_of in your model to make your model easier to use. You could create a class called ModelAndPhone and then have it combine the name and phone during to_s. I know it sounds complicated, but I like it. -- :// Nathan Herald
Nathan, is this how it should be used? Class Zipcode < ActiveRecord::Base def self.city_zip composed_of :cityzip, :mappings => w%(city zipcode) end end azip = Zipcode_find_by_zipcode(''98055'') azip.city_zip I''m getting a ''method not found'' error. -Larry On 7/26/06, Nathan Herald <myobie@gmail.com> wrote:> > On 2006-07-26 00:05:32 -0400, Kevin Olbrich > <devlists-rubyonrails@devlists.com> said: > > > > > On Tuesday, July 25, 2006, at 10:52 PM, Philip Hallstrom wrote: > >>> What is the best way to create a drop down where the viewable text in > a > >>> concatenation of 2 or more columns? > >>> > >>> For instance, I hane a lookup table with these columns. > >>> Model FOO > >>> columns: id , name, phone > >>> > >>> In my drop select tag, I''d like the user to see: > >>> > >>> "name1 phone1" > >>> "name2 phone2" > >>> etc.. > >>> > >>> I know I can do this using find_by_sql . .. > >>> But, isn''t there a more elegant way? > >> > >> class FOO > >> def name_plus_phone > >> self.name + '' '' + self.phone > >> end > >> end > >> > >> @names = FOO.find(:all) > >> > >> <%= select_tag ''blah'', options_from_collection_for_select(@names > >> ''id'', ''name_plus_phone'') %> > >> > >> -philip > >> _______________________________________________ > >> Rails mailing list > >> Rails@lists.rubyonrails.org > >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > <%= select ''model'', ''name'', @names.map {|x| ["#{x.name} - #{x.phone}", > > x.id]} %> > > > > This works too, and set''s the ''name_id'' attribute to the selected id. > > > > _Kevin > > www.sciwerks.com > > You also can use composed_of in your model to make your model easier to > use. You could create a class called ModelAndPhone and then have it > combine the name and phone during to_s. > > I know it sounds complicated, but I like it. > -- > > :// > Nathan Herald > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060727/51997c25/attachment.html