4

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

4

2 回答 2

9

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

class OU < OrganizationUser
end

或者

class OrgUser < OrganizationUser
end
于 2012-06-22T13:45:03.527 回答
1

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

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

第1步

在你的库中创建一个 alias.rb。其中将包含您的别名映射和持有这些映射的常量

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
 => #<Home id: nil, created_at: nil, updated_at: nil> 
于 2012-06-23T03:33:02.627 回答