Techioz Blog

Ruby Rails 7.1.3 での引数の数が間違っています (1 が指定され、0 が期待されます) [クローズ済み]

概要

Service クラスの category.name で並べ替えるスコープを作成しようとしています。 カテゴリはサービス カテゴリの名前を含むモデルであり、カテゴリ名フィールドで並べ替えたいのですが、次のエラーが発生します。 引数の数が間違っています (1 が指定され、0 が期待されます)

それは間違っています?

Rails 7.1.3 で mysql order を呼び出す方法をすべて変更しましたが、同じエラーが発生します。

スコープコードは次のとおりです。

class Service < ApplicationRecord
  before_save :delete_photo, if: ->{ remove_photo == "1" && 
  photo.attached? }

  attr_accessor :remove_photo

  belongs_to :company
  belongs_to :branch, optional: true
  belongs_to :family
  belongs_to :subfamily
  belongs_to :gender_type, optional: true
  belongs_to :category, optional: true
  belongs_to :service_location, optional: true
  belongs_to :base_service, class_name: 'Service', optional: true
  has_many :additional_features, class_name: 
 'ServiceAdditionalFeature', dependent: :destroy
  has_and_belongs_to_many :branches
  has_and_belongs_to_many :employees
  has_one_attached :photo
  has_one_attached :video

  validate :photo_format
  validate :video_format

  after_create :set_sequence_number_after_create

  scope :with_company, ->(company) { where company: company }
  scope :with_branch, ->(branch) { where branch: branch }
  scope :with_owner_or_assigned_branch, ->(branch) { joins("LEFT 
  JOIN branches_services ON branches_services.service_id = 
  services.base_service_id AND branches_services.branch_id = # 
 {branch.present? && branch.id.present? ? branch.id : -1}")
                                                
.where("IFNULL(services.branch_id, -1) = #{branch.present? && 
branch.id.present? ? branch.id : -1} AND 
(services.base_service_id IS NULL OR 
branches_services.service_id IS NOT NULL)") }

scope :left_join_branch, ->(branch) { joins("LEFT JOIN 
branches_services ON branches_services.service_id = services.id 
AND branches_services.branch_id = #{branch.present? && 
branch.id.present? ? branch.id : -1}").select("services.*, 
IF(branches_services.branch_id IS NOT NULL, 1, 0) AS 
is_selected") }

scope :inner_join_employee, ->(employee) { 
joins(:employees).where(employees: { id: employee }).distinct }
scope :left_join_employee, ->(employee) { joins("LEFT JOIN 
employees_services ON employees_services.service_id = 
services.id AND employees_services.employee_id = # 
{employee.present? ? employee.id : -1}").select("services.*, 
IF(employees_services.employee_id IS NOT NULL, 1, 0) AS 
is_selected") }
scope :with_gender_type, ->(gender_type) { where("(# 
{gender_type} = 3 AND gender_type_id IS NULL) OR gender_type_id 
= #{gender_type}") }
scope :with_category, ->(category) { where category: category }
scope :ordered, -> { order recurrent: :desc }
scope :inner_join_subfamily, -> { 
joins(:family).joins(:subfamily).select("services.*, 
CONCAT(IFNULL(CONCAT(services.internal_reference_code, ' - '), 
''), services.name) AS fullname, families.id AS family_id, 
families.name AS family_name, subfamilies.id AS subfamily_id, 
subfamilies.name AS subfamily_name" ) }


  scope :left_join_category, -> { joins("LEFT JOIN categories ON categories.id = services.category_id").select("services.*, CONCAT(IFNULL(CONCAT(services.internal_reference_code, ' - '), ''), services.name) AS fullname, IFNULL(categories.id,  -1) AS category_id, IFNULL(categories.name, '--') AS category_name" ) }

  scope :ordered_by_subfamily, -> { order("families.name ASC, subfamilies.name ASC, CONCAT(IFNULL(CONCAT(services.internal_reference_code, ' - '), ''), services.name) ASC") }

  scope :ordered_by_category, -> { order("categories.name ASC") }

  scope :ordered_by_service, -> { order("CONCAT(IFNULL(CONCAT(services.internal_reference_code, ' - '), ''), services.name) ASC") }



  

そして、それはコントローラー内で使用されます。

  def set_services_by_company
      @services_categories = Hash.new


  services = Service.with_company(@user.company_default).with_branch(nil).left_join_category.left_join_branch(@branch).ordered_by_category

解決策

長いスコープチェーンの中で

services = Service.with_company(@user.company_default)
                  .with_branch(nil)
                  .left_join_category
                  .left_join_branch(@branch)
                  .ordered_by_category

left_join_branch(@branch) を引数を指定して呼び出しますが、left_join_branch スコープは引数を受け入れません。

スコープを次のように変更します

services = Service.with_company(@user.company_default)
                  .with_branch(nil)
                  .left_join_category
                  .left_join_branch
                  .ordered_by_category

すでに問題が解決されている可能性があります。