简体   繁体   English

Rails 4.2自联接模型-Create方法返回错误

[英]Rails 4.2 self-join model — Create method returns error

I have a simple self-join model for an Account. 我有一个简单的帐户自加入模型。 An Account can have a single parent and/or multiple child Accounts. 一个帐户可以有一个父帐户和/或多个子帐户。

Here is the class: 这是课程:

class Account < ActiveRecord::Base
  has_many :children, class_name: "Account", foreign_key: "parent_id"
  belongs_to :parent, class_name: "Account"
end

and the migration: 和迁移:

class CreateAccounts < 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

When the create method is invoked on the controller, I get the following error: 在控制器上调用create方法时,出现以下错误:

Account(#70188397277860) expected, got String(#70188381177720)

and it references the first line of the create method in the controller: 它引用控制器中create方法的第一行:

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

Since the Account model is self-referential, it seems like Rails expects an Account as an argument for constructing an Account. 由于Account模型是自引用的,因此Rails似乎希望将Account作为构造Account的参数。

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. 《 Rails ActiveRecord指南》有一个有限的示例,我相信我已经密切关注了,但是我无法弄清楚哪里出了问题。

I have tried various permutations foreign key types and what-not with no luck. 我尝试了各种排列外键类型,但没有运气。 Any pointers are appreciated. 任何指针表示赞赏。

EDIT: 编辑:

Here is the form, generated by the scaffold command, that gathers the information for creating a new account. 这是由scaffold命令生成的表单,该表单收集了用于创建新帐户的信息。 As suggested by @SteveTurczyn in the comments, the form is collecting a string for the parent field instead of an id. 正如@SteveTurczyn在评论中所建议的,表单正在收集父字段的字符串而不是ID。

<%= form_for(@account) do |f| %>
  <% if @account.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@account.errors.count, "error") %> prohibited this account from being saved\
:</h2>

      <ul>
      <% @account.errors.full_messages.each do |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: 编辑2:

Changing the parent field from text_field to number_field has no effect on the result. parent字段从text_field更改为number_field不会影响结果。

The parameters passed to the create method are the same: 传递给create方法的参数是相同的:

{"utf8"=>"✓",
"authenticity_token"=>"pq0sp162cA7Bmn7uw67F7gOvUVLj/S+xcasVibqysiF68vheVkATsf4pwKgPqH5nawjc0BnIj3qoot8JyIeVmg==",
 "account"=>{"parent"=>"0",
 "name"=>"Foo",
 "category"=>"Bar"},
 "commit"=>"Create Account"}

I'm a little confused about how this is supposed to work. 我对此应该如何工作感到有些困惑。

You're submitting the ID of a parent from the form, but your association expects parent to actually be an Account object, not the ID of one. 您正在从表单中提交parent的ID,但是您的协会希望parent实际上是一个Account对象,而不是一个ID。

Change the form to submit parent_id instead of parent . 更改表单以提交parent_id而不是parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM