title: Editing a single attribute with more than one form field
body: <p><strong>Background:</strong> Users and communities share a </p>
  
  <blockquote>
    <p>has_many :through</p>
  </blockquote>
  
  <p>relationship. Each community has a "community_type" string that identifies it (ie "Gender", "City", etc.). </p>
  
  <p><strong>Objective:</strong> In my edit form, I'd like to allow the user to edit his :community_ids based on community type. Something like:</p>
  
  <blockquote>
  <pre><code>&lt;%= form_for current_user do |f| %&gt;
  &lt;%= f.collection_select(:community_ids, Community.filtered_by("Gender"), :id, :name) %&gt;
  &lt;%= f.collection_select(:community_ids, Community.filtered_by("City"), :id, :name) %&gt;
  &lt;% end %&gt;
  </code></pre>
  </blockquote>
  
  <p>The issue is that the form only accepts the last form field value for :community_ids - in this case being the "City" - rather than merging all of them as one big :community_ids array.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>For those interested, I ended up refactoring my model code from the answers below to this:</p>
  
  <pre><code>  %W[ community1 community2 community3 community4 ].each do |name|
      define_method "#{name}" do
        self.communities.filtered_by("#{name}").map(&amp;:name)
      end
      define_method "#{name}_ids" do
        self.communities.filtered_by("#{name}").map(&amp;:id)
      end
      define_method "#{name}_ids=" do |val|
        self.community_ids += val
      end
    end
  </code></pre>