Tag: 超类

类的超类不匹配用户 – 从ActiveRecord :: Baseinheritance

我试图找出我的超类错配错误。 我读过的所有post都描述了这个问题,即User在我的应用程序中被定义为两次类。 就我而言,它没有定义两次。 我有一个服务文件夹,其中有一个用户文件夹(用于用户服务类)。 在该用户文件夹中,我有一个名为organisation_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 我认为以我的方式定义服务类应该没问题,因为它inheritance自ActiveRecord :: […]

Ruby可以子类化实例变量_overwrite_超类(同名)吗?

在“ruby编程语言”一书的第7.3.5节“inheritance和实例变量”中说: 因为实例变量与inheritance无关,所以子类使用的实例变量不能“遮蔽”超类中的实例变量。 如果子类使用与其祖先之一使用的变量同名的实例变量,它将覆盖其> ancestor变量的值。 这可以有意地完成,以改变祖先的行为,或者可以无意中完成。 在后一种情况下,几乎可以肯定会导致错误。 与前面描述的私有方法的inheritance一样,这是另一个原因,当您熟悉(并控制)超类的实现时,扩展Ruby类是安全的。 我做了自己的测试,但似乎来自子类的实例变量不会影响超类 我的Ruby版本 bob@bob-ruby:~$ ruby –version ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux] bob@bob-ruby:~$ 以下是代码 class Point attr_accessor :x,:y def initialize(x,y) @x,@y=x,y end end class Point3D p=Point.new(1,2) => # irb(main):053:0> q=Point3D.new(4,5,6) => # irb(main):054:0> qx => 4 irb(main):055:0> px => 1 irb(main):056:0>