Brian Cardarella
2006-May-27 18:11 UTC
[Rails] Saving an Array to MySQL... data changes...
I have a multiple select menu: <%= select_tag ''resume[employer_1_industry][]'', options_for_select(industries_helper, @resume.employer_1_industry), :multiple=> true %> It produces this HTML: <select id="resume[employer_1_industry][]" multiple="multiple" name="resume[employer_1_industry][]"> <option value="">- Select -</option> <option value="1">Electrical Engineering - Analog</option> <option value="2">Electrical Engineering - Digital</option> <option value="3">Electrical Engineering - RF</option></select> When I process the form the array from the multiple select produced is: ["1", "3"] or whatever the options are that I select. The problem seems to be when I process the form. MySQL saves the array as: --- - "1" - "3" I have the column set to ''text'' datatype. I have tried everything from forcing the column back to an array (.to_a) to forcing it to a string before it is written to MySQL but nothing seems to work. The data can''t be read back into my app properly. Any help? -- Posted via http://www.ruby-forum.com/.
Brian Cardarella
2006-May-27 20:50 UTC
[Rails] Re: Saving an Array to MySQL... data changes...
I found the answer... must serialize the column data in the model... serialize :employer_1_industry, Array Brian Cardarella wrote:> I have a multiple select menu: > > <%= select_tag ''resume[employer_1_industry][]'', > options_for_select(industries_helper, @resume.employer_1_industry), > :multiple=> true %> > > It produces this HTML: > > > <select id="resume[employer_1_industry][]" multiple="multiple" > name="resume[employer_1_industry][]"> > <option value="">- Select -</option> > <option value="1">Electrical Engineering - Analog</option> > <option value="2">Electrical Engineering - Digital</option> > <option value="3">Electrical Engineering - RF</option></select> > > When I process the form the array from the multiple select produced is: > > ["1", "3"] or whatever the options are that I select. The problem seems > to be when I process the form. MySQL saves the array as: > > --- > - "1" > - "3" > > I have the column set to ''text'' datatype. I have tried everything from > forcing the column back to an array (.to_a) to forcing it to a string > before it is written to MySQL but nothing seems to work. The data can''t > be read back into my app properly. Any help?-- Posted via http://www.ruby-forum.com/.
njmacinnes@gmail.com
2006-May-28 09:09 UTC
[Rails] Saving an Array to MySQL... data changes...
I don''t understand why your using an array. Why not use a relational model, with an employers table and an industries table, then a habtm join using an employers_industries table? -N On 27/05/06, Brian Cardarella <brian@powercareers.com> wrote:> I have a multiple select menu: > > <%= select_tag ''resume[employer_1_industry][]'', > options_for_select(industries_helper, @resume.employer_1_industry), > :multiple=> true %> > > It produces this HTML: > > > <select id="resume[employer_1_industry][]" multiple="multiple" > name="resume[employer_1_industry][]"> > <option value="">- Select -</option> > <option value="1">Electrical Engineering - Analog</option> > <option value="2">Electrical Engineering - Digital</option> > <option value="3">Electrical Engineering - RF</option></select> > > When I process the form the array from the multiple select produced is: > > ["1", "3"] or whatever the options are that I select. The problem seems > to be when I process the form. MySQL saves the array as: > > --- > - "1" > - "3" > > I have the column set to ''text'' datatype. I have tried everything from > forcing the column back to an array (.to_a) to forcing it to a string > before it is written to MySQL but nothing seems to work. The data can''t > be read back into my app properly. Any help? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Brian Cardarella
2006-Jun-04 17:20 UTC
[Rails] Re: Saving an Array to MySQL... data changes...
I don''t need a relational model here... just saving simple data. unknown wrote:> I don''t understand why your using an array. Why not use a relational > model, with an employers table and an industries table, then a habtm > join using an employers_industries table? > -N-- Posted via http://www.ruby-forum.com/.