Rails Engine:使用应用程序类扩展模型

发布于 2024-09-05 06:43:47 字数 394 浏览 5 评论 0原文

我在供应商/插件中有一个引擎。我的问题是,我似乎无法使用基础应用程序中的模型来扩展引擎模型。

我的文件夹结构:

APPNAME
 -app
  -models
   -item.rb
 -vendor
  -plugins
   -image_gallery
    -app
     -models
      -image_gallery.rb

没什么特别的...在我的 image_gallery.rb 中我只有这个:

class ImageGallery < Item
end

但是 Rails 抱怨缺少 item.rb 中定义的方法。如果我在 image_gallery.rb 中定义它们,它就可以工作。

I Have an engine in vendor/plugins. My problem is, that i seemingly can´t extend the engine-model with a model in the base application.

My folder structure:

APPNAME
 -app
  -models
   -item.rb
 -vendor
  -plugins
   -image_gallery
    -app
     -models
      -image_gallery.rb

Nothing special... in my image_gallery.rb i have just this:

class ImageGallery < Item
end

But Rails complains about missing methods which are defined in item.rb. If i define them in the image_gallery.rb, it works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2024-09-12 06:43:47

您可以尝试在 development.rb 中设置 config.cache_classes = true

You can try to set config.cache_classes = true in your development.rb

奶气 2024-09-12 06:43:47

我相信这是一个加载顺序问题。在开发模式下不会重新加载供应商目录。当您的引擎模型尝试继承应用程序中的模型时,该模型尚未加载。

抱歉,我不知道解决此问题的方法,也从未找到解决方法。我认为 Rails 3 将解决这个问题。我们通常只是将相关模型保留在同一个引擎中,或者将引擎内容移到主应用程序中以避免它。这很糟糕,我很想知道正确的解决方案。

在生产模式下尝试一下,应该不会有问题。如果是这种情况,您可以考虑使用霰弹枪。

I believe this is a load order problem. The vendor directory doesn't get reloaded in development mode. The model in your app hasn't been loaded when you engine model tries to inherit from it.

Sorry I don't know a fix for this and have never found one. I think rails 3 will be addressing this. We normally just keep the related models in the same engine or move the engine stuff up into the main app to avoid it. This sucks and I would love to know the proper solution.

Try in production mode and you shouldn't have the issue. You could look at using shotgun if this is the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文