Forum: Ruby on Rails multiple select_tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mich (Guest)
on 2006-04-11 18:38
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
Jeff Everett (Guest)
on 2006-04-11 20:08
(Received via mailing list)
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
mich (Guest)
on 2006-04-12 13:18
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
Jeff Everett (Guest)
on 2006-04-12 20:32
(Received via mailing list)
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
mich (Guest)
on 2006-04-13 15:10
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
Jeff Everett (Guest)
on 2006-04-13 18:02
(Received via mailing list)
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/HowtoUseSe...

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
This topic is locked and can not be replied to.