Active Record 模型中的列信息

发布于 2024-09-19 18:04:14 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夕嗳→ 2024-09-26 18:04:14

从应用程序文件夹中获取所有模型。

models = []
Dir.foreach("#{RAILS_ROOT}/app/models") do |model_path|
  if FileTest.directory?(model_path)
    next
  else
    filename = File.basename(model_path, '.rb')
    models << filename.camelize.constantize \
      if ["ActiveRecord::Base"].include?(filename.camelize.constantize.superclass.to_s)\
      && filename.camelize.constantize.table_exists?
  end
end

这将显示具有数据类型的所有列。

for model in models
  puts model
  model.columns_hash.select {|column_name,column_type| puts column_name + ":" + column_type }
end

也许这会对你有帮助?

To fetch all the model from app folder.

models = []
Dir.foreach("#{RAILS_ROOT}/app/models") do |model_path|
  if FileTest.directory?(model_path)
    next
  else
    filename = File.basename(model_path, '.rb')
    models << filename.camelize.constantize \
      if ["ActiveRecord::Base"].include?(filename.camelize.constantize.superclass.to_s)\
      && filename.camelize.constantize.table_exists?
  end
end

This will display all the columns with datatype.

for model in models
  puts model
  model.columns_hash.select {|column_name,column_type| puts column_name + ":" + column_type }
end

May be this will help you ??

橘味果▽酱 2024-09-26 18:04:14

您可能正在考虑 annotate_models 插件。

You're probably thinking of the annotate_models plugin.

幽蝶幻影 2024-09-26 18:04:14

There is the annotate_models gem. This is the version I once used, but can't tell if is still mantained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文