【发布时间】:2016-10-24 21:42:33
【问题描述】:
我正在尝试找出我的超类不匹配错误。我读过的所有关于此的帖子都将问题描述为 User 在我的应用程序中被定义为一个类两次。
就我而言,它没有定义两次。我有一个服务文件夹,其中有一个用户文件夹(用于用户服务类)。在该用户文件夹中,我有一个名为 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
除此之外,我的用户模型将用户定义为:
class User < ApplicationRecord
我认为按照我的方式定义服务类应该没问题,因为它继承自 ActiveRecord::Base 而不是 ApplicationRecord。
谁能看到我在这里做错了什么?我还能在哪里寻找用户的第二个定义?
采纳塞尔吉奥的建议
我将用户组织映射器服务改为如下打开:
class User::OrganisationMapperService < ActiveRecord::Base
但这会给我的 Users::OrgRequestsController 一个错误,它的新定义如下:
def new
@all_organisations = Organisation.select(:title, :id).map { |org| [org.title, org.id] }
@org_request = OrgRequest.new#form(OrganisationRequest::Create)
matched_organisation = User::OrganisationMapperService.new(current_user).matching_organisation
@org_request.organisation_id = matched_organisation.try(:id)
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
我不知道该怎么做,因为如果有来自用户的继承,我显然有一个用户。
【问题讨论】:
标签: ruby-on-rails ruby inheritance activerecord superclass