<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -79,9 +79,20 @@ module AwesomeFields
     #   &lt;tt&gt;find(:all)&lt;/tt&gt; on the class of the first object of the collection.
     #   If there is no object in the collection, then an attempt will be made to
     #   find out the right class to call +find+ on by attempting to reflect on
-    #   the association represented by the method name (if any)
+    #   the association represented by the method name (if any).
     # * The list of selected options (or the one selected option) will be
     #   derived by calling +method+ on the form object.
+    # * If there is an association on the given method, then it will be
+    #   reflected on to determine if the :multiple option should be set (so that
+    #   multiple items may be selected in the select box).
+    # * Finally, if the method corresponds to an association, the method name
+    #   will be changed to either method_ids or method_id as long as the
+    #   value_method is unchanged. Since to_param will give the value, this can
+    #   be assigned directly into an id list without any additional handling.
+    #   This allows us to mark the field as an id field and allow Rails to take
+    #   care of the assignment by just passing the params in server-side. If you
+    #   want to disable this behavior, just pass a :value_method parameter in
+    #   (even if it's still :to_param).
     #
     # This is based on a few larger assumptions:
     # * The collection is homogeneous (i.e., all objects are of the same class).
@@ -103,23 +114,40 @@ module AwesomeFields
     #
     # Other options are passed on to the +select+ method.
     def collection_field(method, opts = {}, html_options = {})
+      assoc = @object.class.reflect_on_association(method)
+
       selected = opts[:selected] || @object.send(method)
       selected = [ selected ] if ! selected.respond_to?(:first)
-      reference_object = opts[:collection] ? opts[:collection].first \
-                                           : selected.first
-      reference_class = (reference_object &amp;&amp; reference_object.class) ||
-                        @object.class.reflect_on_association(method).klass
+      ref_obj  = opts[:collection] ? opts[:collection].first : selected.first
+      ref_class = (ref_obj &amp;&amp; ref_obj.class) || (assoc &amp;&amp; assoc.klass)
 
       value_meth = opts[:value_method] || :to_param
-      text_meth =  opts[:text_method]  || text_method_for(reference_object)
-      collection = opts[:collection]   || reference_class.find(:all)
+      text_meth  = opts[:text_method]  || text_method_for(reference_object)
+      collection = opts[:collection]   || ref_class ? ref_class.find(:all) : []
+      multiple   = opts[:multiple]
+
+      # Below, we switch multiple on if we have a *_many relationship on this
+      # method and it hasn't already been set. We also switch the method to be
+      # either method_ids or method_id, depending on the association. This lets
+      # the assignment happen correctly once the form data returns to the
+      # controller, thus eliminating the need for special handling.
+      if assoc
+        if assoc.macro.to_s =~ /_many$/
+          multiple = true unless multiple.nil?
+
+          method = &quot;#{method.to_s.singularize}_ids&quot; unless opts[:value_method]
+        elsif assoc.macro.to_s == 'belongs_to'
+          method = &quot;#{method.to_s}_id&quot; unless opts[:value_method]
+        end
+      end
 
       all_values = collection.collect do |item|
         [ item.send(text_meth), item.send(value_meth) ]
       end
-      selected_values = selected.collect { |item| item.send(value_meth) }
+      selected_vals = selected.collect { |item| item.send(value_meth) }
 
-      self.select(method, all_values, opts.merge(:selected =&gt; selected_values),
+      self.select(method, all_values,
+                  opts.merge(:selected =&gt; selected_vals, :multiple =&gt; multiple),
                   html_options)
     end
 </diff>
      <filename>lib/awesome_fields/awesome_field_helpers.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>d70c5452c69baf58fc11c73ab33494ecd5725e30</id>
    </parent>
  </parents>
  <author>
    <name>Antonio Salazar Cardozo</name>
    <email>savedfastcool@gmail.com</email>
  </author>
  <url>http://github.com/Shadowfiend/awesome_fields/commit/3ad3dc49b65b70278176d6b4418aff485269a235</url>
  <id>3ad3dc49b65b70278176d6b4418aff485269a235</id>
  <committed-date>2008-11-11T06:39:56-08:00</committed-date>
  <authored-date>2008-11-11T06:39:56-08:00</authored-date>
  <message>collection_field now guesses at the :multiple option and automatically switches a method to _id or _ids depending on the association for easier server-side handling.</message>
  <tree>b8b0d2a5a8d41ff81461596eefef363527f803fc</tree>
  <committer>
    <name>Antonio Salazar Cardozo</name>
    <email>savedfastcool@gmail.com</email>
  </committer>
</commit>
