Ticket #52 (reopened defect)

Opened 6 years ago

Last modified 5 years ago

HABTM collection_select()

Reported by: openface Owned by: somebody
Priority: normal Milestone:
Component: Action View Version:
Severity: normal Keywords:
Cc:

Description

Looking at a HABTM between employees and departments...

<?=collection_select("employee", "departments", $departments, "id", "name", array(), array("multiple"=>true))?>

I would expect this to display a multi-select (which it does), and be able to treat a HABTM association.

A couple of issues:

Firstly, when you pass "multiple" param, the name of the select input should be appended with parenthesis to allow for array passing via request:

<select name="employee[departments]">

Should become:

<select name="employee[departments][]">

Secondly, the values are being ignored when using collection_select() like this. In my case, I have an employee which belongs to 2 departments. The above call to collection_select() should not only display the multiple-select (which it does), but it should have the associated department IDs selected. (which it does not)

Am I using this function in the wrong way, or is the functionality just not built yet to handle HABTM with form helpers. ?

Many thanks..

Change History

  Changed 6 years ago by openface

Perhaps this isn't a defect/bug as much as it is a feature request.

Rails offers the ability to implement additional features as plugins. It's not clear where a feature such as this (multiple select form helper for HABTM support) would fall. (Included as a Trax helper or ??)

  Changed 6 years ago by john

  • status changed from new to closed
  • resolution set to fixed

  Changed 6 years ago by john

  • severity changed from major to normal
  • cc peter@… removed
  • component changed from Router to Action View
  • summary changed from qerpetehfdr to HABTM collection_select()
  • priority changed from low to normal
  • version 0.10.0 deleted
  • milestone 1.0 deleted
  • keywords xzc removed
  • type changed from spam to defect

in reply to: ↑ description   Changed 5 years ago by alderete

Replying to openface:

The above call to collection_select() should not only display the multiple-select (which it does), but it should have the associated department IDs selected. (which it does not) Am I using this function in the wrong way, or is the functionality just not built yet to handle HABTM with form helpers. ?

Reading through the code to try and deal with this exact issue myself, it appears that the collection_select() helper, and all of the select helpers that rely on the options_for_select() method, are only able to handle one selected value, and are explicitly not able to handle HABTM situations.

There are two issues:

1. to_collection_select_tag() calls Helpers->value() to get the value of attribute of the current object. The value() method uses ActiveRecord?->send() to get the value of the attribute. The send() method is only capable of getting values from the object's main table; it actually constructs the SQL itself, and is hard-coded to query only the object's table, not to go through the join table to get the multiple values of a HABTM attribute.

(It's not only not very flexible, it seems inefficient, because it looks like it will do a database query for each attribute, whether the attributes are already loaded into the object or not.)

2. Even if you hack ActiveRecord?->send() to be able to get a HABTM attribute, the way the option tags are added to the select tag is assuming there will only be one selected value. The call chain ends with the options_for_select() tag, which assumes that the $selected value will be a single value; it won't correctly handle the situation where you pass in an array of ActiveRecords? that you would get from the HABTM attribute.

I'm not sure what the right solution here is; I am trying to get this to work right now, but I think I might be better off handling the HABTM attribute manually in my controller method for the page...

Note: See TracTickets for help on using tickets.