title: Create a select from existing or create new in a form Rails 3
body: <p>So I'm trying to set a name attribute of organizations and create a new organization or choose from a previously existing organization from the same form. </p>
  
  <p>I've tried to follow Ryan Bates' railscast on the topic here: <a href="http://railscasts.com/episodes/57-create-model-through-text-field" rel="nofollow">http://railscasts.com/episodes/57-create-model-through-text-field</a></p>
  
  <p>I have also tried numerous solutions from stack. However, I can't quite seem to get it to run (that and I have a validation that does not recognize the virtual attribute I'm using)</p>
  
  <p>so my organizations model:</p>
  
  <pre><code>class Organization &lt; ActiveRecord::Base
    has_many :materials
    has_many :users
    has_and_belongs_to_many :causes
    has_and_belongs_to_many :schools, :join_table =&gt; 'organizations_schools'
  ####The following line has been edited ####
    attr_accessible :name, :unlogged_books_num, :id, :new_organization_name
    attr_accessor :new_organization_name
    before_validation :create_org_from_name
  
    validates_presence_of :name
  
    def self.assign_school_to_organization(org, school)
      orgschool = OrganizationsSchool.create(:organization_id=&gt; org.id, :school_id=&gt; school[0])
    end
  
   def create_org_from_name
     create_organization(:name=&gt;new_organization_name) unless new_organization_name.blank?
   end
  end
  </code></pre>
  
  <p>I have also tried the <code>create_org_from_name</code> as the following: </p>
  
  <pre><code> def create_org_from_name
     self.name = new_organization_name
   end
  </code></pre>
  
  <p>And this does not change the name to the organization name before validating or saving the instance.
  I have also tried to change the <code>before_save</code> to <code>before_validation</code>, and that has not worked</p>
  
  <p>My controller for organization (I also tried to change this in create)</p>
  
  <pre><code>def create
     respond_to do |format|
        @organization = Organization.new(params[:organization])
        @organization.name = @organization.new_organization_name unless @organization.new_organization_name.blank?
        if @organization.save
          @school = params[:school]
          Organization.assign_school_to_organization(@organization, @school)     
          format.html { redirect_to @organization, notice: 'Organization was successfully created.' }
          format.json { render json: @organization, status: :created, location: @organization }
        else
          format.html { render action: "new" }
          format.json { render json: @organization.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>And finally, I have what my form is doing currently:</p>
  
  <pre><code>&lt;%= form_for(@organization) do |f| %&gt;
    &lt;% if @organization.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@organization.errors.count, "error") %&gt; prohibited this organization from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @organization.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;% @schools = School.all %&gt;
    &lt;% @organizations = Organization.all %&gt;
    &lt;div class="field"&gt;
        &lt;%= f.label 'Organization Name' %&gt;&lt;br /&gt;
        &lt;%= f.collection_select(:name, @organizations, :name, :name, :prompt=&gt;"Existing Organization") %&gt;
        Or Create New
        &lt;%= f.text_field :new_organization_name %&gt;
      &lt;/div&gt;
      &lt;div class="field"&gt;
        &lt;%= f.label :unlogged_books_num %&gt;&lt;br /&gt;
        &lt;%= f.number_field :unlogged_books_num %&gt;
      &lt;/div&gt;
      &lt;div class="field"&gt;
        &lt;%= f.label 'School' %&gt;&lt;br /&gt;
        &lt;% school_id = nil %&gt;
        &lt;%= collection_select(:school, school_id, @schools, :id, :name) %&gt;  
      &lt;/div&gt;
      &lt;div class="actions"&gt;
        &lt;%= f.submit %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>==================================EDIT============================================
  So currently, when I try to make an organization with something only written in the virtual text field, My log tells me the following: </p>
  
  <pre><code>Processing by OrganizationsController#create as HTML
    Parameters: {"utf8"=&gt;"✓",    "authenticity_token"=&gt;"igoefz8Rwm/RHrHLTXQnG48ygTGLydZrzP4gEJOPbF0=", "organization"=&gt;  {"name"=&gt;"", "new_organization_name"=&gt;"Virtual Organization", "unlogged_books_num"=&gt;""},   "school"=&gt;["1"], "commit"=&gt;"Create Organization"}
    Rendered organizations/_form.html.erb (7.1ms)
    Rendered organizations/new.html.erb within layouts/application (8.0ms)
  Completed 200 OK in 17ms (Views: 12.2ms | ActiveRecord: 1.0ms)
  </code></pre>
  
  <p>================================EDIT 2============================================
  So this is what I get from the rails console if I try to create a new organization running this command: <code>Organization.create(:new_organization_name=&gt;"Virtual Organization", :unlogged_books_num=&gt;"3")</code></p>
  
  <pre><code>irb(main):001:0&gt; Organization.create(:new_organization_name=&gt;"Virtual Organization",    :unlogged_books_num=&gt;"3")
     (0.1ms)  BEGIN
     (0.1ms)  ROLLBACK
  =&gt; #&lt;Organization id: nil, name: nil, unlogged_books_num: 3, created_at: nil, updated_at: nil&gt;
  </code></pre>
  
  <p>If the function of <code>create_org_from_name</code> is <code>self.name = new_organization_name</code>, then the result of the same command from the console is blank:</p>
  
  <pre><code>irb(main):002:1&gt; Organization.create(:new_organization_name=&gt;"Virtual Organization", :unlogged_books_num=&gt;"3")
  irb(main):003:1&gt; 
  </code></pre>