Hi all, I have a column in a MySQL DB of the SET type (defined as ... SET(''red'', ''green'', ''blue'') not null ...). The scaffold displays just a label but no select field or check boxes. Is it not handling columns of this type? I thought a nice way to enable editing of a this kind of information would be a multiple select field. I tried generating it like this: <label>Features:</label><br /> <select multiple size="5" name="my_obj[colours]" id="my_obj_colours"> <%= options_from_collection_for_select( @my_obj.colours.split('',''), ''to_s'', ''to_s'') %> </select> Upon inspection of the submitted @params[''my_obj''] I only see one of the values that had been selected. How would I get an array of all the selected ones? I would then construct a string from the array so that I can insert it into the SET field. Any ideas? Many thanks in advance! -- Nicky
On Sun, 27 Feb 2005 22:27:51 +0100, Nickolay Kolev <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote:> Hi all, > > I have a column in a MySQL DB of the SET type (defined as ... > SET(''red'', ''green'', ''blue'') not null ...). > > The scaffold displays just a label but no select field or check boxes. > Is it not handling columns of this type? > > I thought a nice way to enable editing of a this kind of information > would be a multiple select field. I tried generating it like this: > > <label>Features:</label><br /> > <select multiple size="5" name="my_obj[colours]" id="my_obj_colours"> > <%= options_from_collection_for_select( > @my_obj.colours.split('',''), ''to_s'', ''to_s'') %> > </select> > > Upon inspection of the submitted @params[''my_obj''] I only see one of > the values that had been selected. How would I get an array of all the > selected ones? I would then construct a string from the array so that I > can insert it into the SET field. > > Any ideas?You should probably not use the SET, ENUM or other non-sql data types available to you in MySQL. What you probably want is a colours table with a primary key and a ''description'' field. Then either use has_and_belongs_to_many, has_many or any of the other associations to handle it.> Many thanks in advance! > > -- Nicky > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On Feb 27, 2005, at 4:14 PM, Michael Koziarski wrote:> On Sun, 27 Feb 2005 22:27:51 +0100, Nickolay Kolev > <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote: >> Hi all, >> >> I have a column in a MySQL DB of the SET type (defined as ... >> SET(''red'', ''green'', ''blue'') not null ...). >> >> The scaffold displays just a label but no select field or check boxes. >> Is it not handling columns of this type? >> >> I thought a nice way to enable editing of a this kind of information >> would be a multiple select field. I tried generating it like this: >> >> <label>Features:</label><br /> >> <select multiple size="5" name="my_obj[colours]" id="my_obj_colours"> >> <%= options_from_collection_for_select( >> @my_obj.colours.split('',''), ''to_s'', ''to_s'') %> >> </select> >> >> Upon inspection of the submitted @params[''my_obj''] I only see one of >> the values that had been selected. How would I get an array of all the >> selected ones? I would then construct a string from the array so that >> I >> can insert it into the SET field. >> >> Any ideas? > > You should probably not use the SET, ENUM or other non-sql data types > available to you in MySQL. What you probably want is a colours table > with a primary key and a ''description'' field. Then either use > has_and_belongs_to_many, has_many or any of the other associations to > handle it.Another option, if the values in your set aren''t very dynamic, is to encode them in the model class and use a varchar type for the column. class Palette < ActiveRecord::Base serialize :colors, Set def self.valid_colors %w(red green blue) end end Then, in your view: <%= select("palette", "colors", Palette.valid_colors, {}, "multiple" => "multiple") %> When you''re editing, this will automagically select the values present in the set. The controller code is the trickiest bit (IMO) def create @palette = Palette.new(@params[''palette'']) @palette.colors = Set.new @request.params[''palette[colors]''] # save and render ... end def update @palette = Palette.find(@params[''palette''][''id'']) @params[''palette''][''colors''] = Set.new @request.params[''palette[colors]''] # update and render ... end The one thing that I don''t understand is why you must say @request.params[''palette[colors]''] as opposed to @params[''palette''][''colors'']. Looking at the structure of the CGI request data: @request.params={"palette[colors]"=>["red", "blue"]} @params={"action"=>"create", "palette"=>{"colors"=>"red"}, "controller"=>"palettes"} Why does the @params hash only get the first value? Also, if there''s a more elegant way to write the above code (maybe using method_missing or something), please let me know. I''m pretty new to ruby/rails.
On Sun, 27 Feb 2005 19:43:04 -0800, Jon Bauman <jon-rails-63aXycvo3TyHXe+LvDLADg@public.gmane.org> wrote:> On Feb 27, 2005, at 4:14 PM, Michael Koziarski wrote: > > > On Sun, 27 Feb 2005 22:27:51 +0100, Nickolay Kolev > > <nmkolev-OhoefBWHl6Eb1SvskN2V4Q@public.gmane.org> wrote: > >> Hi all, > >> > >> I have a column in a MySQL DB of the SET type (defined as ... > >> SET(''red'', ''green'', ''blue'') not null ...). > >> > >> The scaffold displays just a label but no select field or check boxes. > >> Is it not handling columns of this type? > >> > >> I thought a nice way to enable editing of a this kind of information > >> would be a multiple select field. I tried generating it like this: > >> > >> <label>Features:</label><br /> > >> <select multiple size="5" name="my_obj[colours]" id="my_obj_colours"> > >> <%= options_from_collection_for_select( > >> @my_obj.colours.split('',''), ''to_s'', ''to_s'') %> > >> </select> > >> > >> Upon inspection of the submitted @params[''my_obj''] I only see one of > >> the values that had been selected. How would I get an array of all the > >> selected ones? I would then construct a string from the array so that > >> I > >> can insert it into the SET field. > >> > >> Any ideas? > > > > You should probably not use the SET, ENUM or other non-sql data types > > available to you in MySQL. What you probably want is a colours table > > with a primary key and a ''description'' field. Then either use > > has_and_belongs_to_many, has_many or any of the other associations to > > handle it. > > Another option, if the values in your set aren''t very dynamic, is to > encode them in the model class and use a varchar type for the column. > > class Palette < ActiveRecord::Base > serialize :colors, Set > def self.valid_colors > %w(red green blue) > end > end > > Then, in your view: > > <%= select("palette", "colors", Palette.valid_colors, {}, "multiple" => > "multiple") %> > > When you''re editing, this will automagically select the values present > in the set. The controller code is the trickiest bit (IMO) > > def create > @palette = Palette.new(@params[''palette'']) > @palette.colors = Set.new @request.params[''palette[colors]''] > # save and render ... > end > > def update > @palette = Palette.find(@params[''palette''][''id'']) > @params[''palette''][''colors''] = Set.new > @request.params[''palette[colors]''] > # update and render ... > end > > The one thing that I don''t understand is why you must say > @request.params[''palette[colors]''] as opposed to > @params[''palette''][''colors'']. Looking at the structure of the CGI > request data: > > @request.params={"palette[colors]"=>["red", "blue"]} > @params={"action"=>"create", "palette"=>{"colors"=>"red"}, > "controller"=>"palettes"} > > Why does the @params hash only get the first value?You need to name the parameter colors[] to have multiple values, otherwise @params will only take one of them.> Also, if there''s a more elegant way to write the above code (maybe > using method_missing or something), please let me know. I''m pretty new > to ruby/rails. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
On Feb 27, 2005, at 7:50 PM, Michael Koziarski wrote:>> Then, in your view: >> >> <%= select("palette", "colors", Palette.valid_colors, {}, "multiple" >> => >> "multiple") %> >> >> When you''re editing, this will automagically select the values present >> in the set. The controller code is the trickiest bit (IMO) >> >> def create >> @palette = Palette.new(@params[''palette'']) >> @palette.colors = Set.new @request.params[''palette[colors]''] >> # save and render ... >> end >> >> def update >> @palette = Palette.find(@params[''palette''][''id'']) >> @params[''palette''][''colors''] = Set.new >> @request.params[''palette[colors]''] >> # update and render ... >> end >> >> The one thing that I don''t understand is why you must say >> @request.params[''palette[colors]''] as opposed to >> @params[''palette''][''colors'']. Looking at the structure of the CGI >> request data: >> >> @request.params={"palette[colors]"=>["red", "blue"]} >> @params={"action"=>"create", "palette"=>{"colors"=>"red"}, >> "controller"=>"palettes"} >> >> Why does the @params hash only get the first value? > > You need to name the parameter colors[] to have multiple values, > otherwise @params will only take one of them.If I write the select element by hand and set name="colors[]", it works, but is it possible to do this with the select helper? I tried this: <%= select("palette", "colors", Palette.valid_colors, {}, "multiple" => "multiple") %> I get <select id="palette_colors[]" multiple="multiple" name="palette[colors[]]"> <option>red</option> <option>green</option> <option>blue</option> </select> which results in @parameters={"action"=>"create", "palette"=>{"colors[]"=>"red"}, "controller"=>"palettes"}