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?
defined? ModelName
defined? ModelName
will return "constant" if model defined.
Since defined?
is problematic (see @Jiggneshh Gohel's comment), perhaps you can check the filenames in the models
dir.
files = Dir[Rails.root + 'app/models/*.rb']
models = files.map{ |m| File.basename(m, '.rb').camelize }
models.include? "User" => true
Another option is use exists
Return false if there is no column in the model.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.