Я создаю самоссылочные отношения для сервисов. Идея состоит в том, чтобы позволить вам добавить потенциально бесконечный уровень вспомогательных услуг в службу, чтобы у служб могли быть дети, и у этих детей могли быть дети и т. Д.
Для этого я создал два класса, Службыи SubServices. SubServices - это простая таблица соединений с 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
Новое действие SubService:
def new
@sub_service = @service.child_services.new
respond_to do |format|
format.html
format.js { render :new }
end
end
SubService create action:
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
Новое представление Sub Service:
<%= render 'form', f: f %>
<% end %>
_form:
<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>
Thisошибка, которую возвращает моя консоль
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)