轨道模型的别名

我在rails应用程序中的模型名称是OrganizationUser ,有没有办法为OU或OrgUser创建此模型的别名,以便我可以在rails控制台中使用..

如果kishie的答案不适合您,您可以创建另一个inheritance自OrganizationUser的模型:

 class OU < OrganizationUser end 

要么

 class OrgUser < OrganizationUser end 

为更清洁的一面工作。 假设你有一个模型

  class Home < ActiveRecord::Base class << self def agent p "This is a Dummy String" end end end 

步骤1

在lib中创建一个alias.rb。 其中包含您的Alias映射和持有这些映射的常量

 module Alias C = Home #to make a alias of class H = Home.new #a class object alias end 

第2步

转到轨道c

 rails c "inside it for loading" Loading development environment (Rails 3.2.1) ruby-1.9.3-preview1 :001 > require 'alias' => true ruby-1.9.3-preview1 :002 > include Alias => Object ruby-1.9.3-preview1 :003 > C => Home(id: integer, created_at: datetime, updated_at: datetime) ruby-1.9.3-preview1 :004 > H => #