简体   繁体   English

Rails:hook_for :orm 没有找到 active_record

[英]Rails: hook_for :orm not finding active_record

I'm writing a custom generator.我正在编写一个自定义生成器。 For the most part, the generator is able to use hooks successfully.在大多数情况下,生成器能够成功使用钩子。 For instance,例如,

hook_for :resource_route, in: :rails, required: true

Invokes resource_route as expected.按预期调用resource_route However:然而:

hook_for :orm, in: :rails, required: true

Returns the error:返回错误:

error  active_record [not found]

I'm assuming this is because the active_record_generator is located in a dramatically different directory from other generators, such as the resource_route generator.我假设这是因为active_record_generator位于与其他生成器(例如resource_route生成器)截然不同的目录中。

rails / activerecord / lib / rails / generators / active_record.rb rails / activerecord / lib / rails / generators / active_record.rb

rails / railties / lib / rails / generators / rails / resource_route / resource_route_generator.rb rails / railties / lib / rails / generators / rails / resource_route / resource_route_generator.rb

Is there a way to get my generator to properly hook active record?有没有办法让我的生成器正确挂钩活动记录?

I eventually hacked this, only after I managed a dodgy work around using hook_for and remove_hook_for fiasco.我最终破解了这个问题,只有在我使用hook_forremove_hook_for了一次狡猾的工作之后。 My advise leave it alone it's not worth the trouble.我的建议是不要管它,这不值得麻烦。

This way uses much less code for way more result for your effort.这种方式使用更少的代码为您的工作带来更多的结果。

There are a few tricks you may want to use.您可能需要使用一些技巧。

  1. Stay within the Rails::Generators namespace.留在 Rails::Generators 命名空间内。

    The folder structure I used was:我使用的文件夹结构是:

     lib/generators/my_own_model/ templates/ my_own_model_generator.rb
  2. Set the config in the model for the generator you want to create在模型中为要创建的生成器设置配置

    That's all you need to overcome the error whatever [not found] headaches. error whatever [not found]令人头疼的问题,这就是克服error whatever [not found]所需的全部内容。

    Code looks like this代码看起来像这样

    require 'rails/generators/active_record/model/model_generator' module Rails module Generators hide_namespace 'my_own_model' class Railtie < ::Rails::Engine if config.respond_to?(:app_generators) config.app_generators.orm = :my_own_model else config.generators.orm = :my_own_model end end class MyOwnModelGenerator < ActiveRecord::Generators::ModelGenerator source_root "#{base_root}/active_record/model/templates" # all public methods will get executed by rails g protected # these won't but can be overwritten by sub classes private # these are still exposed to your templates nifty end end end
  3. The third trick is to hide your generator name.第三个技巧是隐藏你的生成器名称。 Why is that you ask?你为什么这么问? Well you may like to know that you won't be needing it anymore.好吧,您可能想知道您不再需要它了。

     $ rails g model MyNewModel generator:belongs_to my_own:boolean

You have just created a default generator which you can overwrite again from your template folders.您刚刚创建了一个默认生成器,您可以从模板文件夹中再次覆盖它。 =) =)

nJoy!快乐!

至少在 Rails 5 中,你需要指定生成器的类型 (:model, :migration, :application_record)

hook_for :orm, as: :model

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM