Tag: sti

Rails STI:如何更改“类型”列的类名和值之间的映射

由于公司规则,我不能使用我们的域名类; 我打算用一个类比来代替。 我有一个名为projects的表,其中有一个名为’type’的列,可能的值为’indoor’和’outdoor’。 具有室内和室外的记录具有明显的function分离,并且作为STI实现非常巧妙地适合。 不幸的是,我无法更改类型名称,也无法在全局命名空间中添加类。 有没有办法为’type’指定不同的值? 注意:我不是要为STI使用不同的列名而不是’type’。 我希望类型的值不同于我的类名。

带有rails 3的多表inheritance

在rails 3中进行多表inheritance时,是否有标准或最佳实践? 到目前为止,我能找到的最好的文章是: http://mediumexposure.com/multiple-table-inheritance-active-record/ 但即使这需要一些更改(例如将需求转移到初始化程序而不是旧的/config/environment.rb) 有更好的资源/标准吗?

Rails中的单表inheritance4

我试图在Rails 4中实现一个简单的STI,但是我还有一些东西无法实现。 我有以下课程: class Person < ActiveRecord::Base end class NaturalPerson < Person end class LegalPerson < Person end class Employee < NaturalPerson end class Customer < NaturalPerson end 问题是,我有一些属性,我只想从Employee类访问,有些只来自Customer等,但我找不到方法。 如果我要使用Rails 3的方式,我会用attr_accesible解决它。 但现在这是不可能的,因为我既不使用attr_accesiblegem,也不愿意。

Rails Sti:单路径,不同的控制器

有STI课程: class Page < ActiveRecord::Base belongs_to :user end class FirstTypePage < Page end class SecondTypePage < Page end 每个class级的控制器, class PageController < AplicationCorroller end class FirstTypePageController < PageController end class SecondTypePageController < PageController end 和路线: resources :user resource :page end 如何通过FirstTypePageController处理FirstTypePage,SecondTypePage由单一路径上的SecondTypePageController处理? 即 user / 1 / page / 2由以下处理:FirstTypePageController如果“page 2”类型是“FirstTypePage”,则通过SecondTypePageController如果“page 2”类型是“SecondTypePage”? 更新:我的解决方案: match ‘user/:user_id/page/:action’, :controller=>’page/first_type_page’, […]

使用FactoryGirl测试简单的STI

我有一个类,这是其他一些专门处理行为的类的基础: class Task < ActiveRecord::Base attr_accessible :type, :name, :command validates_presence_of :type, :name, :command # some methods I would like to test end CounterTask类inheritance自Task class CounterTask < Task end 这一切都正常,直到我尝试测试基类,因为它必须有一个类型。 FactoryGirl.define do factory :task do sequence(:name) { |n| “name_#{n}” } sequence(:command) { |n| “command_#{n}” } end end 您如何测试超类的基本function?