RoR ActiveRecord find_by_sql 问题

发布于 2024-08-06 15:06:22 字数 2784 浏览 3 评论 0原文

在回答这个问题时,我想先声明一下,我对 Ruby 开发相当陌生,但就我个人而言,我一直致力于自己寻找答案,而不是在论坛上异想天开地提出问题。话虽这么说,这是我的第一篇正式“帖子”,我已尽一切努力在我的帖子中保持准确和简洁。

我有一个正在连接的 MSSQL 2005 数据库,使用 RoR ActiveRecord & RubyGems ADO。

在我的 AR 基类中,我正在运行 find_by_sql 语句,该语句正在执行多表 SELECT 语句。我的查询正在按预期执行,并且我已验证返回的数据是否准确。

我的目标

我需要查询 MSSQL 2005 数据库,然后将返回的数据转储到 .csv 文件。

我的计划是:

  1. 使用红宝石宝石和宝石。 ADO 连接到 MSSQL DB
  2. 使用 Ruby AR 使用 find-by-sql 查询数据库(由于查询的性质)
  3. 使用 Ruby FasterCSV 检索数据并将其转储到 .csv 文件。

当前场景:

我能够成功连接到数据库,并使用 AR 查询数据。

问题

运行 find-by-sql 查询后,AR 将返回哈希数组中的数据。 正如我之前所说,我是 AR 新手,并且已经在 RoR 中进行了几个月的开发。因此,我不知道如何访问数组的属性。

这是我到目前为止所得到的:

require 'rubygems'
require 'activerecord'
require "fastercsv"


ActiveRecord::Base.pluralize_table_names = false

ActiveRecord::Base.establish_connection(
    :adapter => 'sqlserver',
    :mode => 'ODBC',
    :dsn => 'xxxx',
    :database => 'xxxx',
    :username => 'blah',
    :password => 'blahblah',
    :host => 'server'
)

class Dp_display < ActiveRecord::Base
    od30 = Dp_display.find_by_sql("SELECT dd.acct_no, dd.cur_bal, dd.od_dt, ra.email_1
    FROM dp_display dd, rm_acct ra
    WHERE dd.rim_no = ra.rim_no and dd.cur_bal < -10 and dd.class_code = 125 and 
    dd.od_dt = convert(varchar, getdate()-30,101)
    ORDER BY dd.od_dt desc");

    #testing od30 returning values successfully
    puts od30

结果:

当我查看 od30 时,所有数据都正确返回。数据如下所示(仅列出第一个数组元素):

    od30 = Array (2 elements)
      +[0] = {Dp_display}#<Dp_display:0x7667d74>
          +@attributes = Hash (4 elements)
             +'acct_no'-> 1122334455
             +'email_1'-> [email protected]
             +'cur_bal'-> -333.55
             +'od_dt'-> 2009-09-08 00:00:00.000
      +[1] = {Dp_display}#<Dp_display:0x7667d10>

使用 FasterCSV,我执行以下操作:

    FasterCSV.open("c://file30.csv", "w") do |csv|
      csv << od30

csv 文件已成功创建,当我打开它时,它只包含:

<Dp_display:0x7667d74>

我试图获取的是:

1122334455,< a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c4b5453585d487c4b54594e59125f5351">[电子邮件受保护],-333.55,2009-09-08 00:00: 00.000

出于学习/测试的目的,我尝试使用许多不同的数组函数访问数组元素,包括 model.attributes、each_index、flatten,然后放置结果。不幸的是,我无法弄清楚。

无论如何,我得出了相同的结果:

同样,我知道在一段时间内,我可以提出一个复杂的解决方案,但现在,我想尝试保持简单(因为我是开发新手,并尝试学习小步骤)。

任何帮助将不胜感激(即使只是为我指明正确的方向就很好了!)。

谢谢。

I want to preface this question by stating that I am fairly new to Ruby development, however, personally, I've dedicated myself to trying to find the answers myself, as opposed to whimsically asking questions on forums. That being said, this is my first official "post", and I have made every attempt to be as accurate and concise in my posting.

I have a MSSQL 2005 database that I am connecting to, using RoR ActiveRecord & RubyGems ADO.

In my AR Base Class, I am running a find_by_sql statement, which is executing a multi-table SELECT statement. My query is executing as expected, and I have verified that the data being returned is accurate.

My Goal:

I need to query an MSSQL 2005 database, and then dump the data that is returned to a .csv file.

My plan to accomplish this is:

  1. Use ruby gems & ADO to connect to the MSSQL DB
  2. Use Ruby AR to query the database using find-by-sql (due to the nature of the query)
  3. Use Ruby FasterCSV to retrieve the data and dump it to a .csv file.

Current Scenario:

I am able to successfully connect to the database, and query the data using AR.

Problem:

Once the find-by-sql query has run, AR returns the data in an array of hashes.
As I stated before, I am new to AR, and have been developing in RoR for several months now. As such, I do not know how to access the array's attributes.

Here's what I have so far:

require 'rubygems'
require 'activerecord'
require "fastercsv"


ActiveRecord::Base.pluralize_table_names = false

ActiveRecord::Base.establish_connection(
    :adapter => 'sqlserver',
    :mode => 'ODBC',
    :dsn => 'xxxx',
    :database => 'xxxx',
    :username => 'blah',
    :password => 'blahblah',
    :host => 'server'
)

class Dp_display < ActiveRecord::Base
    od30 = Dp_display.find_by_sql("SELECT dd.acct_no, dd.cur_bal, dd.od_dt, ra.email_1
    FROM dp_display dd, rm_acct ra
    WHERE dd.rim_no = ra.rim_no and dd.cur_bal < -10 and dd.class_code = 125 and 
    dd.od_dt = convert(varchar, getdate()-30,101)
    ORDER BY dd.od_dt desc");

    #testing od30 returning values successfully
    puts od30

Result:

When I look at od30, all data is being returned correctly. The data looks like this (only listing the first array element):

    od30 = Array (2 elements)
      +[0] = {Dp_display}#<Dp_display:0x7667d74>
          +@attributes = Hash (4 elements)
             +'acct_no'-> 1122334455
             +'email_1'-> [email protected]
             +'cur_bal'-> -333.55
             +'od_dt'-> 2009-09-08 00:00:00.000
      +[1] = {Dp_display}#<Dp_display:0x7667d10>

Using FasterCSV, I perform the following:

    FasterCSV.open("c://file30.csv", "w") do |csv|
      csv << od30

The csv file is successfully created, and when I open it, it only contains:

<Dp_display:0x7667d74>

What I am trying to get is:

1122334455,[email protected],-333.55,2009-09-08 00:00:00.000

For learning / testing purposes, I have tried accessing the array elements using many different array functions including model.attributes, each_index, flatten, and then putting the result. Unfortunately, I cannot figure it out.

In any case, I come up with the same result:

Again, I know that with some time, I could come up with an elaborate solution, but for now, I want to try and keep it simple (as I am new to development, and trying to learn small steps).

Any assistance would be greatly appreciated (even if simply pointing me in the right direction would be great!).

Thank you.

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

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

发布评论

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

评论(2

对不⑦ 2024-08-13 15:06:22

find_by_sql 仍会尝试将您的结果映射到对象,在您的情况下是 Dp_display 对象。由于它们是 ActiveRecord 对象,因此您可以像任何属性一样访问它返回的列,但表中不存在的任何派生列将是只读的。

看来您正在尝试转储对象数组,而不是单独转储每个对象。尝试这样的事情:

FasterCSV.open("c://file30.csv", "w") do |csv|
  csv << ['here', 'are', 'my', 'headers']
  od30.each |o|
     csv << [o.acct_no, o.email_1, o.cur_bal, o.od_dt]
  end
end

这超出了我的想象,可能有一种更简单的方法可以通过将标头键与属性相匹配来实现。祝你好运!

同行

find_by_sql will still attempt map your results to objects, in your case Dp_display objects. As they are ActiveRecord objects you can access the columns it returned like any attribute, but any derived columns that don't exist in the table will be read-only.

It appears you are trying to dump an array of objects instead of each object on its own. Try something like this:

FasterCSV.open("c://file30.csv", "w") do |csv|
  csv << ['here', 'are', 'my', 'headers']
  od30.each |o|
     csv << [o.acct_no, o.email_1, o.cur_bal, o.od_dt]
  end
end

That is off the top of my head, there is probably an easier way to do it by matching header keys to the attributes. Good luck!

Peer

难以启齿的温柔 2024-08-13 15:06:22

或者只是

FasterCSV.open("c://file30.csv", "w") do |csv|
  od30.each |o|
     csv << o
  end
end

http://fastercsv.rubyforge.org/

Or just

FasterCSV.open("c://file30.csv", "w") do |csv|
  od30.each |o|
     csv << o
  end
end

http://fastercsv.rubyforge.org/

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