Ruby on Rails中一个模型的完全限定名是什么?

时间:2023-01-02 20:24:08

If I have 2 different classes with name User, say one in FooModule and other as a model in app/models/user.rb, how do I make sure I am using the correct one?

如果我有两个不同的类,名称用户,一个在FooModule,另一个作为模型在app/models/ User。rb,我怎么确定我用的是正确的?

EDIT: FooModule::User would definitely give me the correct one.

编辑:FooModule::用户一定会给我正确的。

What I had meant to ask was: If ApplicationController includes FooModule, would User or ::User still give me app/models/user?

我想问的是:如果ApplicationController包含了FooModule, User还是会给我app/model / User吗?

1 个解决方案

#1


6  

Refer to them using their full names, FooModule::User and ::User

使用他们的全名:FooModule: User和:User

Generally if you just use User, it should assume you mean ::User, unless you are within FooModule. Either way, use FooModule::User or ::User to be sure.

通常,如果您只是使用用户,它应该假定您的意思是::用户,除非您在FooModule内。无论哪种方式,都要使用FooModule::User或::User来确保。

#1


6  

Refer to them using their full names, FooModule::User and ::User

使用他们的全名:FooModule: User和:User

Generally if you just use User, it should assume you mean ::User, unless you are within FooModule. Either way, use FooModule::User or ::User to be sure.

通常,如果您只是使用用户,它应该假定您的意思是::用户,除非您在FooModule内。无论哪种方式,都要使用FooModule::User或::User来确保。