Hello again, I have a table called line_items, with the following fields: id int item_name varchar() qty int conditions text I''ve made my LineItem model, and everything works as such (with simple text_fields).. Now I''d like for the conditions field to be a select_tag() with multiple choice possibility. I''m guessing I need some code to expand/restract the array for the conditions - or maybe not ? Any help much appreciated, /mich -- Posted via http://www.ruby-forum.com/.
This will get you a multi-select for an array of conditions: select_tag( ''conditions[]'', options_for_select( [ [ ''Cond1'', 1 ], [ ''Cond2'', 2 ], [ ''Cond2'', 3 ] ] ), :multiple => true ) The conditions array will be available in params[:conditions] options_for_select will take any container, so you could easily pull those values from the database if that''s where you have them. Hope that helps... Jeff On 4/11/06, mich <nospam@thanks.com> wrote:> Hello again, > > I have a table called line_items, with the following fields: > > id int > item_name varchar() > qty int > conditions text > > I''ve made my LineItem model, and everything works as such (with simple > text_fields).. > Now I''d like for the conditions field to be a select_tag() with multiple > choice possibility. > > I''m guessing I need some code to expand/restract the array for the > conditions - or maybe not ? > > Any help much appreciated, > > /mich > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jeff Everett wrote:> This will get you a multi-select for an array of conditions: > > select_tag( > ''conditions[]'', > options_for_select( [ [ ''Cond1'', 1 ], [ ''Cond2'', 2 ], [ ''Cond2'', 3 ] ] > ), > :multiple => true ) > > The conditions array will be available in params[:conditions] > > options_for_select will take any container, so you could easily pull > those values from the database if that''s where you have them.Hmm.. What I''d to achieve is a way for a line_item to have more than one condition. So let''s say that the line_item: Car - can have a condition as NEW and USED. I''m uncertain how to tell the model to wrap those two conditions into something I can stick into my database table. In addition, the model should also be able to "expand" the information from the table, before parsing it to the view/controller. Any ideas ? /mich -- Posted via http://www.ruby-forum.com/.
It sounds like you want a Has-And-Belongs-To-Many (habtm) relationship between line_items and conditions. You can check out: http://jrhicks.net/96 for a pretty good tutorial on how to set those up in the database and getting that info into a view. Or search this archive or google for habtm Jeff On 4/12/06, mich <nospam@thanks.com> wrote:> Jeff Everett wrote: > > This will get you a multi-select for an array of conditions: > > > > select_tag( > > ''conditions[]'', > > options_for_select( [ [ ''Cond1'', 1 ], [ ''Cond2'', 2 ], [ ''Cond2'', 3 ] ] > > ), > > :multiple => true ) > > > > The conditions array will be available in params[:conditions] > > > > options_for_select will take any container, so you could easily pull > > those values from the database if that''s where you have them. > > Hmm.. What I''d to achieve is a way for a line_item to have more than one > condition. So let''s say that the line_item: Car - can have a condition > as NEW and USED. > > I''m uncertain how to tell the model to wrap those two conditions into > something I can stick into my database table. > In addition, the model should also be able to "expand" the information > from the table, before parsing it to the view/controller. > > Any ideas ? > > /mich > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jeff Everett wrote:> It sounds like you want a Has-And-Belongs-To-Many (habtm) relationship > between line_items and conditions. You can check out: >Well, not exactly. The condtions "model" is fixed and will not change, so I was not considering sticking it into a table. I''ve used your previous example above, and I have the conditions[] array available in the controller, where I can do a: params[:conditions].each { |con| @line_item.condition << con } Which works. I could probably delimit it with fx. ''::'' - so my table field would look like so: NEW::USED::RETAIL - and when grabbing it from db write a similar mechanism to extract each condition. However, I am not too pleased about having this code in the controller, it would be more appropiate having it in the model (line_items) So how do work with the params[:conditions] array in the model ? By the way, many thanks for your help so far ! /mich -- Posted via http://www.ruby-forum.com/.
I still think you''d probably be better off using a habtm relationship for this, it would certainly be the most Rails-ish way to go about it. ( It always worries when someone tells me a business rule is "fixed" 8) ) But having said that, if you''re using MySQL what you''re describing is a lot like the SET column type, which is not standard SQL. There are some tips and options for using this with Rails here: http://wiki.rubyonrails.com/rails/pages/HowtoUseSetAndEnumColumns My gut feeling is if you go down this route, it''s going to be very difficult to not duplicate code in different parts of the app, and you will probably end up having to override rather low level Rails methods ( like save and update ), or write various custom methods to replace them. But that''s just my 2?... Jeff On 4/13/06, mich <nospam@thanks.com> wrote:> Jeff Everett wrote: > > It sounds like you want a Has-And-Belongs-To-Many (habtm) relationship > > between line_items and conditions. You can check out: > > > > Well, not exactly. The condtions "model" is fixed and will not change, > so I was not considering sticking it into a table. > > I''ve used your previous example above, and I have the conditions[] array > available in the controller, where I can do a: > > params[:conditions].each { |con| @line_item.condition << con } > > Which works. I could probably delimit it with fx. ''::'' - so my table > field would look like so: NEW::USED::RETAIL - and when grabbing it from > db write a similar mechanism to extract each condition. However, I am > not too pleased about having this code in the controller, it would be > more appropiate having it in the model (line_items) > > So how do work with the params[:conditions] array in the model ? > > By the way, many thanks for your help so far ! > > /mich > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >