Activerecord find_by_sql 不输出SQL查询结果

发布于 2025-01-01 07:46:36 字数 340 浏览 0 评论 0原文

我正在使用 activerecordfind_by_sql 输出 sql 查询的结果:

S = Object.find_by_sql("SELECT * FROM foo")
S.each do |s|
  puts "#{s}"
end

我得到

#<Object:0x0000010214d5e0>
#<Object:0x0000010214ce60>

等等...

我需要实际结果。

预先感谢

马克

I am using activerecord and find_by_sql to output the results of the sql query:

S = Object.find_by_sql("SELECT * FROM foo")
S.each do |s|
  puts "#{s}"
end

I get

#<Object:0x0000010214d5e0>
#<Object:0x0000010214ce60>

etc...

I need the actual results.

Thanks in advance

Mark

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

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

发布评论

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

评论(4

好听的两个字的网名 2025-01-08 07:46:36

ActiveRecord find_by_sql 函数期望查询将从调用它的类的基础表中返回值。例如,如果您有一个名为 Foo 的类(其基础表 foos 包含列 barbaz),您可以这样做:

Foo.find_by_sql("select * from foos").each do |record|
    puts "Got a Foo: bar=#{record.bar}, baz=#{record.baz}"
end

如果问题是您不喜欢尝试打印对象时获得的输出 (#),那么您只需创建一个类上的 to_s 方法:

class Foo < ActiveRecord::Base
    def to_s
        "Foo bar=#{record.bar}, baz=#{record.baz}"
    end
end

或者,不要直接打印对象 ("#{s}"),使用 inspect

puts s.inspect

The ActiveRecord find_by_sql function expects that the query will return values from the underlying table of the class that it was invoked upon. For example, if you have a class called Foo (with underlying table foos with columns bar and baz) you could do this:

Foo.find_by_sql("select * from foos").each do |record|
    puts "Got a Foo: bar=#{record.bar}, baz=#{record.baz}"
end

If the problem is that you don't like the output you are getting when you try to print out an object (#<Object:0x0000010214d5e0>), then you need only create a to_s method on your class:

class Foo < ActiveRecord::Base
    def to_s
        "Foo bar=#{record.bar}, baz=#{record.baz}"
    end
end

Alternately, don't print the object directly ("#{s}"), use inspect:

puts s.inspect
还如梦归 2025-01-08 07:46:36

如果您只需要来自任意 SQL 查询的原始未处理数据,则应该使用 select_rows 因此:

SomeModel.connection.select_rows('select * from foo').each do |row|
  # `row` is an array of strings at this point
  puts row.join(', ')
end

您必须自己整理类型转换等,但有时所有 ActiveRecord 机制都会得到这样您就可以根据需要使用原始 SQL 和结果。

If you want just the raw unprocessed data from an arbitrary SQL query, you should be using select_rows thusly:

SomeModel.connection.select_rows('select * from foo').each do |row|
  # `row` is an array of strings at this point
  puts row.join(', ')
end

You'll have to sort out type conversions and such yourself but sometimes all the ActiveRecord machinery just gets in the way so you can work with raw SQL and results as needed.

自由如风 2025-01-08 07:46:36

该对象没有 to_s 方法。
您可以尝试使用 puts s.inspectp s 代替

There is no to_s method for that object.
You can try puts s.inspect or p s instead

黑凤梨 2025-01-08 07:46:36

puts 通过调用对象上的 to_s 方法将 ruby​​ 对象转换为字符串。
默认的 to_s 打印对象的类和对象 ID 的编码。
为了打印人类可读的对象形式,请使用 inspect

locs = Location.find_by_sql('select * from locations')
  Location Load (0.5ms)  select * from locations


locs.each do |l|
  # it calls to_s method on object
  puts l
end

#<Location:0x000000055bb328>
#<Location:0x000000055bb058>

locs.each do |l|
  puts l.inspect # prints actual object
end

#<Location id: 15, name: "Annettaside3", street: "71838 Ritchie Cape", city: "East Destanystad", state: "Utah", zip: "58054", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:17:26", updated_at: "2012-01-25 11:17:26", country_name: "Korea">
#<Location id: 16, name: "Sporerbury4", street: "73057 Jerad Shoal", city: "South Kyliefurt", state: "Delaware", zip: "46553-3376", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:24:48", updated_at: "2012-01-25 11:24:48", country_name: "Australia">

puts converts ruby object into string by invoking to_s method on object.
The default to_s prints the object's class and an encoding of the object id.
In order to print human readable form of object use inspect

locs = Location.find_by_sql('select * from locations')
  Location Load (0.5ms)  select * from locations


locs.each do |l|
  # it calls to_s method on object
  puts l
end

#<Location:0x000000055bb328>
#<Location:0x000000055bb058>

locs.each do |l|
  puts l.inspect # prints actual object
end

#<Location id: 15, name: "Annettaside3", street: "71838 Ritchie Cape", city: "East Destanystad", state: "Utah", zip: "58054", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:17:26", updated_at: "2012-01-25 11:17:26", country_name: "Korea">
#<Location id: 16, name: "Sporerbury4", street: "73057 Jerad Shoal", city: "South Kyliefurt", state: "Delaware", zip: "46553-3376", phone: 123456, other_phone: 987654, staff_strength: 40, is_active: true, created_at: "2012-01-25 11:24:48", updated_at: "2012-01-25 11:24:48", country_name: "Australia">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文