Rails 5如何获取每个表中所有行的计数

时间:2017-02-08 18:46:23

标签: ruby-on-rails-5

我有一个旧的脚本在rails 4上为我工作

Imports System.Xml

Module Module1

    Sub Main()
        Dim notebook As XDocument = <?xml version="1.0"?>
                                    <one:Notebooks xmlns:one="http://schemas.microsoft.com/office/onenote/2013/onenote">
                                        <one:Notebook name="Notebook" nickname="Notebook" ID="**********" path="**********" lastModifiedTime="**********" color="**********">
                                            <one:Section name="SectionName1" ID="**********" path="**********" lastModifiedTime="**********" color="**********">
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="1"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="1"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="1"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                                <one:Page ID="**********" name="**********" dateTime="**********" lastModifiedTime="**********" pageLevel="2"/>
                                            </one:Section>
                                        </one:Notebook>
                                    </one:Notebooks>

        Dim xmlDoc As New XmlDocument
        xmlDoc.LoadXml(notebook.ToString())

        Dim nsm As New XmlNamespaceManager(xmlDoc.NameTable)
        nsm.AddNamespace("one", "http://schemas.microsoft.com/office/onenote/2013/onenote")

        For Each node As XmlNode In xmlDoc.DocumentElement.SelectNodes("//one:Notebooks/one:Notebook/one:Section", nsm)
            Console.WriteLine(node.Attributes.ItemOf("name").InnerText)
        Next

        Console.ReadLine()

    End Sub

End Module

但这不会在rails 5项目中返回任何内容:(

1 个答案:

答案 0 :(得分:8)

这应该有效:

  ActiveRecord::Base.connection.tables.map { |t| {t=>  ActiveRecord::Base.connection.execute("select count(*) from #{t}")[0]} }