【问题标题】:show temporary postgres table content in rails在 rails 中显示临时 postgres 表内容
【发布时间】:2020-11-17 08:45:49
【问题描述】:

我有一个由函数创建的临时表,需要显示临时表的内容和标题,因为它总是在变化,是否可以在 rails 中访问这些类型的表?

【问题讨论】:

    标签: ruby-on-rails ruby postgresql ruby-on-rails-5


    【解决方案1】:

    你可以从 Rails 执行 postgresql:

      ActiveRecord.connection.execute("SELECT * FROM my_temporary_table")
    

    或者,如果表名是动态的:

      def my_temporary_table
         // some ruby to generate the table name
      end
    
      ActiveRecord.connection.execute("SELECT * FROM #{my_temporary_table}")
    

    当然,这会生成原始 Postgresql 查询结果,您需要迭代并提取值。您不能以这种方式实例化 ActiveRecord 模型,因为架构是短暂的。但您可以创建 Struct 实例,其行为类似于 Ruby 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      • 2013-06-07
      • 2021-11-04
      相关资源
      最近更新 更多