title: Rails 4.2 self-join model -- Create method returns error
body: <p>I have a simple self-join model for an Account. An Account can have a single parent and/or multiple child Accounts. </p>
  
  <p>Here is the class:</p>
  
  <pre><code>class Account &lt; ActiveRecord::Base
    has_many :children, class_name: "Account", foreign_key: "parent_id"
    belongs_to :parent, class_name: "Account"
  end
  </code></pre>
  
  <p>and the migration:</p>
  
  <pre><code>class CreateAccounts &lt; ActiveRecord::Migration
    def change
      create_table :accounts do |t|
        t.references :parent, index: true
        t.string :name
        t.string :category
        t.timestamps null: false
      end
    end
  end
  </code></pre>
  
  <p>When the create method is invoked on the controller, I get the following error:</p>
  
  <pre><code>Account(#70188397277860) expected, got String(#70188381177720)
  </code></pre>
  
  <p>and it references the first line of the create method in the controller:</p>
  
  <pre><code>def create
    @account = Account.new(account_params)
  
    respond_to do |format|
      if @account.save
        format.html { redirect_to @account, notice: 'Account was successfully created.' }
        format.json { render :show, status: :created, location: @account }
      else
        format.html { render :new }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end
  </code></pre>
  
  <p>Since the Account model is self-referential, it seems like Rails expects an Account as an argument for constructing an Account.</p>
  
  <p>The Rails ActiveRecord Guide has a limited example which I believe I have followed closely, but I cannot figure out where I am going wrong.</p>
  
  <p>I have tried various permutations foreign key types and what-not with no luck. Any pointers are appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>Here is the form, generated by the scaffold command, that gathers the information for creating a new account. As suggested by @SteveTurczyn in the comments, the form is collecting a string for the parent field instead of an id.</p>
  
  <pre><code>&lt;%= form_for(@account) do |f| %&gt;
    &lt;% if @account.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@account.errors.count, "error") %&gt; prohibited this account from being saved\
  :&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @account.errors.full_messages.each do |message| %&gt;
          &lt;li&gt;&lt;%= message %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;div class="field"&gt;
      &lt;%= f.label :parent %&gt;&lt;br&gt;
      &lt;%= f.text_field :parent %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;%= f.label :category %&gt;&lt;br&gt;
      &lt;%= f.text_field :category %&gt;
    &lt;/div&gt;
    &lt;div class="actions"&gt;
      &lt;%= f.submit %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>EDIT2:</p>
  
  <p>Changing the <code>parent</code> field from <code>text_field</code> to <code>number_field</code> has no effect on the result.</p>
  
  <p>The parameters passed to the create method are the same:</p>
  
  <pre><code>{"utf8"=&gt;"✓",
  "authenticity_token"=&gt;"pq0sp162cA7Bmn7uw67F7gOvUVLj/S+xcasVibqysiF68vheVkATsf4pwKgPqH5nawjc0BnIj3qoot8JyIeVmg==",
   "account"=&gt;{"parent"=&gt;"0",
   "name"=&gt;"Foo",
   "category"=&gt;"Bar"},
   "commit"=&gt;"Create Account"}
  </code></pre>
  
  <p>I'm a little confused about how this is supposed to work.</p>