Rails 4.2 self-join model -- Create method returns error -


i have simple self-join model account. account can have single parent and/or multiple child accounts.

here class:

class account < activerecord::base   has_many :children, class_name: "account", foreign_key: "parent_id"   belongs_to :parent, class_name: "account" end 

and migration:

class createaccounts < activerecord::migration   def change     create_table :accounts |t|       t.references :parent, index: true       t.string :name       t.string :category       t.timestamps null: false     end   end end 

when create method invoked on controller, following error:

account(#70188397277860) expected, got string(#70188381177720) 

and references first line of create method in controller:

def create   @account = account.new(account_params)    respond_to |format|     if @account.save       format.html { redirect_to @account, notice: 'account 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 

since account model self-referential, seems rails expects account argument constructing account.

the rails activerecord guide has limited example believe have followed closely, cannot figure out going wrong.

i have tried various permutations foreign key types , what-not no luck. pointers appreciated.

edit:

here form, generated scaffold command, gathers information creating new account. suggested @steveturczyn in comments, form collecting string parent field instead of id.

<%= form_for(@account) |f| %>   <% if @account.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@account.errors.count, "error") %> prohibited account being saved\ :</h2>        <ul>       <% @account.errors.full_messages.each |message| %>         <li><%= message %></li>       <% end %>       </ul>     </div>   <% end %>    <div class="field">     <%= f.label :parent %><br>     <%= f.text_field :parent %>   </div>   <div class="field">     <%= f.label :name %><br>     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :category %><br>     <%= f.text_field :category %>   </div>   <div class="actions">     <%= f.submit %>   </div> <% end %> 

edit2:

changing parent field text_field number_field has no effect on result.

the parameters passed create method same:

{"utf8"=>"✓", "authenticity_token"=>"pq0sp162ca7bmn7uw67f7govuvlj/s+xcasvibqysif68vhevkatsf4pwkgpqh5nawjc0bnij3qoot8jyievmg==",  "account"=>{"parent"=>"0",  "name"=>"foo",  "category"=>"bar"},  "commit"=>"create account"} 

i'm little confused how supposed work.

you're submitting id of parent form, association expects parent account object, not id of one.

change form submit parent_id instead of parent.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -