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

标签 ruby-on-rails self-referencing-table

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

为此,我创建了两个类:Services 和 SubServices。 SubServices 是一个带有parent_service_id 和child_service_id 的简单连接表。我能够在 Rails 控制台中创建所有内容,并且工作得很好。 child 协会和家长协会都在运作。只是在 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

子服务新 View :

  <%= 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)

最佳答案

您想要一个“服务”>“子服务”,然后可能需要“服务”>“子服务”>“子服务”>“子服务”......对吗?您收到未知属性错误,因为 parent_id 位于 SubService 上。您只需使用服务模型就可以做您想做的事。只需将 parent_id 放在上面即可。

然后您就可以摆脱 SubService 模型。

class Service

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

end

关于ruby-on-rails - Ruby on Rails 自引用创建操作返回未知属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58647977/

相关文章:

ruby-on-rails - Rails-如何从 Controller 发送图像

c# - Entity Framework 7 自引用表返回 null

SQL递归+列连接

sql - 自引用表 SQL 查询

ruby-on-rails - 编码::UndefinedConversionError "\xE7"从 ASCII-8BIT 到 UTF-8

ruby-on-rails - 我可以设置 Mongoid 查询超时吗? Mongoid 不会终止长时间查询

ruby-on-rails - 如何在 Rails 3 中测试 cookies.permanent.signed

c# - Entity Framework Core 中的自引用多对多关系

c# - 将类别父 ID 自引用表结构映射到 EF Core 实体

sql - 如何使用 'safe window' 为基于时间的元素创建 MySQL 查询?