Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 类用户的超类不匹配-从ActiveRecord::Base继承_Ruby On Rails_Ruby_Inheritance_Activerecord_Superclass - Fatal编程技术网

Ruby on rails 类用户的超类不匹配-从ActiveRecord::Base继承

Ruby on rails 类用户的超类不匹配-从ActiveRecord::Base继承,ruby-on-rails,ruby,inheritance,activerecord,superclass,Ruby On Rails,Ruby,Inheritance,Activerecord,Superclass,我试图找出我的超类不匹配错误。我读过的所有关于这一点的文章都将问题描述为,在我的应用程序中,用户被定义为两个类 在我的例子中,它没有定义两次。我有一个服务文件夹,其中有一个用户文件夹(用于用户服务类)。在该用户文件夹中,我有一个名为Organization_mapper_service.rb的文件,其中包含: class User < ActiveRecord::Base class OrganisationMapperService def self.call

我试图找出我的超类不匹配错误。我读过的所有关于这一点的文章都将问题描述为,在我的应用程序中,用户被定义为两个类

在我的例子中,它没有定义两次。我有一个服务文件夹,其中有一个用户文件夹(用于用户服务类)。在该用户文件夹中,我有一个名为Organization_mapper_service.rb的文件,其中包含:

class User < ActiveRecord::Base
      class OrganisationMapperService
        def self.call(user: u)
          new(user: user).call
        end

        def initialize(user: u)
          self.user = user
        end

        def call
          if matching_organisation.present?
            # user.organisation_request.new(organisation_id: matching_organisation.id)
            # user.update_attributes!(organisation_id: matching_organisation.id)
          else
            #SystemMailer.unmatched_organisation(user: user).deliver_now
          end
        end

        private

        attr_accessor :user

        def matching_organisation
          User::OrganisationMapperService.new(user).matching_organisation
        end
      end
    end
然后,错误消息显示:

PG::UndefinedTable at /users/4/org_requests/new
ERROR:  relation "user_organisation_mapper_services" does not exist
LINE 8:                WHERE a.attrelid = '"user_organisation_mapper...
**接受塞尔吉奥的建议(确切地说)**

我将我的服务级别更改为:

class User::OrganisationMapperService 
但是我得到了一个错误,它说:

wrong number of arguments (given 1, expected 0)
该错误突出显示了我的服务类的此行:

def initialize(user: u)
      self.user = user
    end
我不知道该怎么办,因为如果从user继承的话,我显然有一个user。

您正在用两个独立的父类定义
user
类。不要那样做

应该是

class User::OrganisationMapperService
这样,将加载并使用现有的
User
类,而不是创建一个新类

我认为可以按照我现有的方式定义服务类,因为它继承自ActiveRecord::Base而不是ApplicationRecord


您的示例中的服务类不会继承任何内容。

即使您解决了所有其他问题,实际上也会有一个无限递归

User::OrganisationMapperService.call(user: User.first)
相当于呼叫:

User::OrganisationMapperService.new(user: User.first).call
它在内部称为匹配组织,因此相当于:

User::OrganisationMapperService.new(user: User.first).matching_organisation
同时,
匹配组织
呼叫

User::OrganisationMapperService.new(user).matching_organisation
它只会一圈一圈地转

唯一的原因是
参数数量错误(给定1,预期为0)
错误。这是因为它应该是
User::organizationmapperservice.new(User:User)
而不是
User::organizationmapperservice.new(User)
方法中的
User::organizationmapperservice.new(User)

更新对评论的回应:

据我所知,
User::OrganizationMapperService
是一个服务类,它负责查找一些
组织,然后执行某种工作

User::organizationMapperService#matching_organization
方法实际上应该包含为给定用户返回匹配组织的代码。实现将完全取决于您如何构建数据库,但我将给出几个示例,让您走上正确的道路,或者给您一些想法

首先,您的
组织
表可能有一个
用户id
列。在这种情况下,您可以对
组织
模型进行简单查询,并使用用户id执行搜索:

class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.where(user_id: user).first
  end
end
或者,您可能有某种类型的联接表,其中一个组织中可能有多个用户(仅在本例中,我们将此表称为“employments”):

最后,
OrganizationMapperService#matching#u Organization
方法变为:

class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.for_user(user).first
  end
end

你的意思是我应该像你一样写它,还是我应该:class User::organizationmapperserviceUser::OrganisationMapperService.new(user).matching_organisation
class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.where(user_id: user).first
  end
end
class Employment < ApplicationRecord
  belongs_to :user
  belongs_to :organisation
end
class Organisation < ApplicationRecord

  has_many :employments
  has_many :users, through: :employments 

  scope :for_user, ->(user) {
    # return organisations belonging to this user
    joins(:users).merge( Employment.where(user_id: user) )
  }

end
class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.for_user(user).first
  end
end