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

Ruby on Rails Self Referential Create Action Returning Unknown Attribute Error

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

为此,我创建了两个 类、服务和子服务。 SubServices 是一个简单的连接 table 与 parent_service_id 和 child_service_id。我能够在 rails 控制台中创建所有内容,并且工作正常。儿童协会和家长协会都在运作。它只是在控制器中发生故障

架构:

  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

service_params:

    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 上。你可以只用服务模型做你想做的事。只需将 parent_id 放在上面即可。

然后您可以摆脱 SubService 模型。

class Service

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

end