I have an affiliates table that has a sponsor_id column. Each affiliate can be assigned a sponsor_id equal to the id of the sponsoring affiliate. If there is no sponsor the sponsor_id is 0. I came up with the following to display the default value of 0 along with the id''s of all the other affiliates in the table. Maybe there should be a :default option or something for the select tag? Seems like this is something that would be pretty common. <%=select(''afiliate'',''sponsor_id'',Affiliate.find(:all, :conditions => ["id != ?",@affiliate.id]).collect {|a| [ a.name, a.id ]}.concat( [[''None'',0]]), :selected => @affiliate.sponsor_id) %>
snacktime wrote:> Maybe there should be a :default option or something for the select > tag? Seems like this is something that would be pretty common.How about... :include_blank => true ...on the select, with not null and a default value of 0 in your database? There''s probably a neater way, but that''s the first one that springs to mind at the moment. Bear in mind I''m still new to this too, so there may be a better way to do it with Rails! ~Dave -- Dave Silvester Rent-A-Monkey Website Development Web: http://www.rentamonkey.com/
snacktime wrote:> I have an affiliates table that has a sponsor_id column. Each > affiliate can be assigned a sponsor_id equal to the id of the > sponsoring affiliate. If there is no sponsor the sponsor_id is 0. I > came up with the following to display the default value of 0 along > with the id''s of all the other affiliates in the table. Maybe there > should be a :default option or something for the select tag? Seems > like this is something that would be pretty common. > > > <%=select(''afiliate'',''sponsor_id'',Affiliate.find(:all, :conditions => > ["id != ?",@affiliate.id]).collect > {|a| [ a.name, a.id ]}.concat( [[''None'',0]]), :selected => > @affiliate.sponsor_id) %>I''ve been doing it by putting this kind of stuff in the controller action: @affilz = Affiliate.find(:all, :conditions => etc etc) @affilz.unshift [[''None'',0]] --Wilson.