I want to know how to check if the model already exists in the project or not?
我想知道如何检查模型是否已经存在于项目中?
When user tries to create a model programatically using the same model name, need to check if it already exists or not?
当用户尝试使用相同的型号名称以编程方式创建模型时,需要检查它是否已存在?
2 个解决方案
#1
21
defined? ModelName
will return "constant" if model defined.
界定?如果定义了模型,ModelName将返回“常量”。
#2
2
Since defined?
is problematic (see @Jiggneshh Gohel's comment), perhaps you can check the filenames in the models
dir.
既然定义了?是有问题的(参见@Jiggneshh Gohel的评论),也许你可以检查模型目录中的文件名。
files = Dir[Rails.root + 'app/models/*.rb']
models = files.map{ |m| File.basename(m, '.rb').camelize }
models.include? "User" => true
#1
21
defined? ModelName
will return "constant" if model defined.
界定?如果定义了模型,ModelName将返回“常量”。
#2
2
Since defined?
is problematic (see @Jiggneshh Gohel's comment), perhaps you can check the filenames in the models
dir.
既然定义了?是有问题的(参见@Jiggneshh Gohel的评论),也许你可以检查模型目录中的文件名。
files = Dir[Rails.root + 'app/models/*.rb']
models = files.map{ |m| File.basename(m, '.rb').camelize }
models.include? "User" => true