Rails 类表继承 gem、假继承和缺失方法

发布于 2024-10-15 02:50:49 字数 422 浏览 5 评论 0原文

我正在修改现有 Rails 应用程序的一部分以使用 Class-Table-Inheritance gem (https://github.com/brunofrank/class-table-inheritance)。一切都很好,除了我在我的超类 Person 中定义了一些实例方法,所有子类都需要能够访问这些方法。例如,full_name(返回连接的名字和姓氏)或 cite_name(返回第一个名字和姓氏)。由于 CTI gem 实际上并不使用 Ruby 继承(所有子类仍然从 ActiveRecord::Base 继承,并且 gem 执行了一些时髦的巫术将 ActiveRecord 字段链接在一起),因此我无法在子类中访问这些方法创建的。关于解决这个问题有什么想法吗?我对 STI 不感兴趣,但我愿意对这个特定的 CTI gem 进行分叉和破解,或者查看其他解决方案。

I'm working on modifying part of an existing Rails app to use the Class-Table-Inheritance gem (https://github.com/brunofrank/class-table-inheritance). All's well, except that I have defined some instance methods in my superclass -- Person, which all subclasses need to be able to access. For instance, full_name, which returns the concatenated first and last names, or cite_name, which returns the first initial and last name. Since the CTI gem doesn't actually use Ruby inheritance (all subclasses still inherit from ActiveRecord::Base, and the gem does some funky voodoo to link the ActiveRecord fields together), I can't access these methods in the subclasses I've created. Any thoughts on working around this? I'm not interested in STI, but I'm willing to either fork and hack on this particular CTI gem, or look at other solutions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心的憧憬 2024-10-22 02:50:49

尝试http://peterhamilton.github.com/citier,它基于CITIEsForRAILS,我分叉了它&让它经历快速发展和各种其他变化。它修复了大部分(如果不是全部)错误,并且工作起来非常简单。

try http://peterhamilton.github.com/citier, it's based on CITIEsForRAILS, I forked it & put it through rapid development and various other changes. It fixes most, if not all the bugs it had and works so simply.

時窥 2024-10-22 02:50:49

就在我的脑海中,您是否看到在您的子类的迁移表中您需要有一行:

create_table :videos, :inherits => :product do |t|

以及在您的 Model.rb 文件中

  class Product < ActiveRecord::Base
    acts_as_superclass # I'm guessing you might be missing this line??
  end

  class Book < ActiveRecord::Base
    inherits_from :product
  end

Just off the top of my head, did you see that in the migration tables of your subclasses you need to have the line:

create_table :videos, :inherits => :product do |t|

and also in your Model.rb files

  class Product < ActiveRecord::Base
    acts_as_superclass # I'm guessing you might be missing this line??
  end

  class Book < ActiveRecord::Base
    inherits_from :product
  end
半寸时光 2024-10-22 02:50:49

我认为全新的 CITIEsForRAILS gem(Class Inheritance & Table Inheritance EmbeddingS For RAILS,请参阅 https://github.com /altrabio/CITIEsForRAILS )正是您想要的。这个宝石扩展了 CTI、STI 和多表继承,同时保留 Ruby 类继承。

I think that the brand new CITIEsForRAILS gem (Class Inheritance & Table Inheritance EmbeddingS For RAILS, see https://github.com/altrabio/CITIEsForRAILS ) does exactly what you desire. This gem extends CTI, STI, & Multi Table Inheritance while preserving Ruby class Inheritance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文