繁体   English   中英

Ruby on Rails 自引用创建操作返回未知属性错误

[英]Ruby on Rails Self Referential Create Action Returning Unknown Attribute Error

我正在为服务创建一个自我参照关系。 这个想法是允许您向服务添加潜在无限级别的子服务,因此服务可以有孩子,这些孩子可以有孩子,等等。

为此,我创建了两个类,Services 和 SubServices。 SubServices 是一个带有 parent_service_id 和 child_service_id 的简单连接表。 我能够在 rails 控制台中创建所有内容,并且效果很好。 孩子和家长协会都有效。 它只是在 controller 中发生故障

架构:

  create_table "services", force: :cascade do |t|
    t.string   "name"
    t.integer  "business_category_id"
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
    t.index ["business_category_id"], name: "index_services_on_business_category_id", using: :btree
  end

  create_table "sub_services", force: :cascade do |t|
    t.integer  "child_service_id"
    t.integer  "parent_service_id"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.index ["child_service_id"], name: "index_sub_services_on_child_service_id", using: :btree
    t.index ["parent_service_id"], name: "index_sub_services_on_parent_service_id", using: :btree
  end

楷模:

class SubService < ApplicationRecord
  belongs_to :parent_service, class_name: 'Service'
  belongs_to :child_service, class_name: 'Service'
end

class Service < ApplicationRecord
  belongs_to :business_category

  has_many :client_services     # TODO REMOVE
  has_many :clients, :through => :client_services   # TODO REMOVE

    has_many :business_services
  has_many :businesses, :through => :business_services

  has_many :parental_services, foreign_key: :parent_service_id, class_name: "SubService"
  has_many :child_services, through: :parental_services

  has_many :children_services, foreign_key: :child_service_id, class_name: "SubService"
  has_many :parent_services, through: :children_services

  validates :name, presence: true
  validates :name, uniqueness: { scope: :business_category, message: "service already added" }

end

子服务新动作:

  def new
    @sub_service = @service.child_services.new
    respond_to do |format|
      format.html
      format.js { render :new }
    end
  end

子服务创建动作:

  def create
    @sub_service = @service.child_services.new(service_params);
    # binding.pry
    if @sub_service.save
      redirect_to(admin_business_categories_path)
    end
  end

服务参数:

    def service_params
      params.require(:sub_service).permit(:name)
    end

子服务新视图:

  <%= render 'form', f: f %>
<% end %>

_形式:

<div class="col-12  col-md-10">
  <div class="col-12  col-md-12">
    <%= f.input :name, label: 'Service Name' %>
  </div>
</div>

<div class="col-12  col-md-2">
  <div class="btn-group-vertical" role="group" aria-label="...">
    <button id="serviceFormCancelButton" class="btn btn-danger">CANCEL</button>
    <%= f.submit 'SAVE', class: 'btn btn-success' %>
    <br>
  </div>
</div>

这是我的控制台返回的错误

ActiveModel::UnknownAttributeError (unknown attribute 'parent_service_id' for Service.):

app/controllers/admin/sub_services_controller.rb:14:in `create'
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.0ms)
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (846.1ms)

您想要一个 Service > SubService,然后可能是 Service > SubService > SubService > SubService.... 对吗? 您收到未知属性错误,因为parent_idSubService上。 只需服务 model,您就可以为所欲为。 只需将parent_id放在上面即可。

然后,您可以摆脱子服务 model。

class Service

 belongs_to :parent_service, foreign_key: :parent_id, class_name: 'Service'
 has_many :child_services, class_name: 'Service'

end

暂无
暂无

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

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