Tag: inheritance

Mongoid store_in产生随机结果

我正在使用Rails 3.2.2和mongoid 2.4.6。 为了保持我的集合小,我使用“store_in”语句将子对象存储在sepparate集合中的基类中。 我的代码如下所示: class BaseClass include Mongoid::Document end class ChildClass1 < BaseClass store_in :child_1 end class ChildClass2 < BaseClass store_in :child_2 end 似乎对象随机存储在或其他子集合中。 Child1类型的对象有时会存储在集合Child2中。 以下是我在日志中看到的令人惊讶的事情: Started POST “/child_class_1” for 127.0.0.1 at 2012-05-22 10:22:51 -0400 Processing by ChildClass1Controller#create as HTML MONGODB (0ms) myproject_development[‘child_2’].insert…. 它来自哪里? 这是mongoid,rails还是mongodb中的错误?

rails中的inheritance和多态关联

我有一个User模型,属于Profile (belongs_to polymorphic)。 一个模型有两个子类,但User中的profile_type始终对应于父模型。 User true SomeProf :profile SomeDeepProf1 < SomeProf SomeDeepProf2 < SomeProf 然后: sdp1 = SomeDeepProf1.new user = sdp1.create_user user.profile_type > ‘SomeProf’ 即使在子类中声明关联, profile_type仍然是SomeProf 。 为什么会这样? 有没有什么方法profile_type匹配子类而不是父类?

ruby中的抽象方法(> = 2.2.0)……它们是否存在?

我正在为接口编写基类。我希望所有inheritance的类实现一些方法,有没有办法让它们成为现实? 我有一个payment_method_base类,我将inheritance。 我希望我所有的payment_method类都实现方法kind() ,它将返回一个字符串,例如’credit_card’或’braintree’或’paypal’或’amazon_pay’ …… 有没有办法确保从payment_method_baseinheritance的类被强制实现方法kind() 请记住,新的payment_method类的创建者在创建类之前可能不知道这些要求。 在java中,这些称为抽象方法。 我想知道ruby是否有类似的东西? https://github.com/shuber/defined/blob/master/lib/defined.rb —-进化 我想知道是否已经修复了允许抽象方法的语言。 —-部分答案这个答案可能是添加我需要的钩子的线索。 是否有类似于inheritance的类#的钩子只在Ruby类定义之后触发? 这并不像我预期的那样工作。 呼叫似乎没有在正确的时间完成。 即便如此,有多少gem处理它。 https://github.com/shuber/defined/blob/master/lib/defined.rb 简单地回答我不能提交,因为这是一个没有真正答案的问题的重复。 def self.abstract(*methods_array) @@must_abstract ||= [] @@must_abstract = Array(methods_array) end def self.inherited(child) trace = TracePoint.new(:end) do |tp| if tp.self == child #modules also trace end we only care about the class end trace.disable missing = ( Array(@@must_abstract) […]

Rails控制器inheritance与关注和混合

我的Rails应用程序中有很多类似的资源,我目前使用控制器inheritance来干掉代码。 我看到控制器文件夹下有一个名为Concer的目录,在那里我可能写出类似的问题(比如归档,激活/去激活等)。我也可以编写mixins。 是否有一种首选方法来干扰控制器代码? 使用inheritance有什么缺点,或使用其他技术有什么优势?

inheritance被误解了

我正在尝试在我的应用程序中构建inheritance。 事实上,我很简单,我有几个模型,其中一些需要“归档”,它只是意味着我将数据从我的数据库公共原理图移动到归档原理图(相同的数据库)。 每个模型都有一个方法’save’itself’OnArchive示例: def saveContactArchive(contact) record = ArchContacts.new record.id=contact.id record.information_id=contact.information_id record.is_internal = contact.is_internal if (contact.is_internal != nil) record.type_contact_id = contact.type_contact_id if (contact.type_contact_id != nil) record.user_id = contact.user_id if (contact.user_id != nil) record.info_readed = contact.info_readed if (contact.info_readed != nil) record.not_received = contact.not_received if (contact.not_received != nil) record.society_name = contact.society_name if (contact.society_name != nil) record.person_name = contact.person_name […]

类的超类不匹配用户 – 从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>

如何将类包含在类中

如何获取类中包含的模块数组,不包括那些通过inheritance传入的模块? 请注意, ancestors , included_modules将不起作用,因为它们的结果不区分包含在超类中预先添加的模块的模块。 换句话说,他们无法区分以下两种情况: M置于B的超类 class A; end class B [B, M, A, Object, Kernel, BasicObject] B M # => -1 B.included_modules # => [M, Kernel] M包含在B class A; end class B [B, M, A, Object, Kernel, BasicObject] B M # => -1 B.included_modules # => [M, Kernel]

Rails – 超类不匹配

使用Rails和控制器inheritance。 我创建了一个名为AdminController的控制器,在/app/controllers/admin/admin_user_controller.rb放置了一个名为admin_user_controller的子类。 这是我的routes.rb namespace :admin do resources :admin_user # Have the admin manage them here. end 应用程序/控制器/管理/ admin_user_controller.rb class AdminUserController < AdminController def index @users = User.all end end 应用程序/控制器/ admin_controller.rb class AdminController < ApplicationController end 我有一个用户模型,我想用管理员权限编辑。 当我尝试连接到: http://localhost:3000/admin/admin_user/ 我收到此错误: superclass mismatch for class AdminUserController

我如何从Rational(或任何没有构造函数的类)inheritance?

我可以很容易地inheritance,例如, String ,例如: class MyString ‘things and stuff’ 但是我如何从没有构造函数的Rationalinheritance? 例如: def MyRat NoMethodError: undefined method `new’ for MyRat:Class MyRat(10).inc # => NoMethodError: undefined method `MyRat’ for main:Object MyRat.send(:initialize, 10).inc # => TypeError: already initialized class # ??? # None of it works! 我找不到初始化新课程的方法。