How to list of all the tables defined for the database when using active record?

后端 未结 5 2196
时光说笑
时光说笑 2020-12-02 05:14

How do I get a list of all the tables defined for the database when using active record?

5条回答
  •  孤城傲影
    2020-12-02 06:06

    It seems like there should be a better way, but here is how I solved my problem:

    Dir["app/models/*.rb"].each do |file_path|
      require file_path # Make sure that the model has been loaded.
    
      basename  = File.basename(file_path, File.extname(file_path))
      clazz     = basename.camelize.constantize
    
      clazz.find(:all).each do |rec|
        # Important code here...
      end
    end
    

    This code assumes that you are following the standard model naming conventions for classes and source code files.

提交回复
热议问题