从引擎模型创建与主应用程序中的模型的belongs_to关系 [英] Creating a belongs_to relationship with a model from the main app from an engine model
问题描述
我为自己构建了一个 Rails 引擎,它要求主应用程序有一个用户表.我需要能够在我的引擎中的模型之一与主应用程序中的用户表之间创建关系.这比仅仅说 belongs_to :user
更复杂吗?我收到一个错误,指出 User
对象为零,但是当我使用控制台时,它返回正确的用户.我的假设是 Rails 假设我的 belongs_to :user
调用意味着与引擎在同一命名空间中的用户类,即 MyEngine::User.有没有办法让我明确指定 User 类位于主应用程序的命名空间中,而不是引擎的命名空间中?
I've built myself a Rails Engine that requires that the main app have a Users table. I need to be able to create a relationship between one of the models in my engine and the Users table in the main app. Is this more complicated than just saying belongs_to :user
? I'm getting an error that says the User
object is nil, but when I use the console it returns the right user. My assumption is that Rails assumed my belongs_to :user
call meant a User's class in the same namespace as the engine, i.e. MyEngine::User. Is there a way for me to explicitly specify that the User class is in the main app's namespace and not the engine's?
推荐答案
结果引擎默认识别 MainApp 的用户类,只需说belongs_to :user.我做错了其他事情:/.
Turns out the engine recognizes the MainApp's user class by default just by saying belongs_to :user. I was doing something else wrong :/.
这篇关于从引擎模型创建与主应用程序中的模型的belongs_to关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!