gpt4 book ai didi

ruby-on-rails - Rails - 对 engine_name 的调用似乎被忽略

转载 作者:行者123 更新时间:2023-12-01 06:34:58 25 4
gpt4 key购买 nike

我想制作一个由两个命名空间隔离的引擎。也就是说,例如,我想制作一个引擎,其类都存在于:

Car::BMW

因此,例如我的模型应该放在:
app/models/car/bmw/

我的表应该以例如为前缀:
car_bmw_

我试图通过在 lib/car/bmw/engine.rb 中使用此代码来实现此目的
module Car
module BMW
class Engine < ::Rails::Engine
isolate_namespace Car::BMW # This will call: engine_name 'car_bmw'
end
end
end

但是,每当我生成模型时,都会使用此代码将该模型放置在:
app/models/car

该表的前缀为:
car_

我究竟做错了什么?我使用的 rails 版本是 4.0.0.beta1
编辑

我在 Rails::Generators::NamedBase 中找到了这个方法
def namespaced_path
@namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }[0]
end

如您所见,它只需要命名空间的第一部分。有人知道为什么是这样吗?

这是 Rails 中的错误还是我不应该让我的类具有双重命名空间?

这是我使用的快速破解方法,用于修复生成器。
require 'rails/generators'

Rails::Generators::NamedBase.class_eval do
protected
def namespaced_class_path
@namespaced_class_path ||= [namespaced_path.split('/')] + @class_path
end

def namespaced_path
@namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }.join('/')
end

def class_name
([file_name]).map!{ |m| m.camelize }.join('::')
end
end

最佳答案

使用这样的类

module Car
module BMW
class Engine < ::Rails::Engine
isolate_namespace Car::BMW # This will call: engine_name 'car_bmw'
paths["app/models"] << "app/models/car/bmw"
end
end
end

将允许您从您指定的子目录加载模型。不过,我不能说这是否会影响生成过程。

还有更多配置选项,请参见例如 here .编辑它们以满足您的需求。

关于ruby-on-rails - Rails - 对 engine_name 的调用似乎被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16088011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com