English 中文(简体)
为什么3号铁路与Mysql2 GemactiveRecord:Base.linkion.execute(sql)返回Array而不是Has?
原标题:Why does Rails 3 with Mysql2 Gem ActiveRecord::Base.connection.execute(sql) return Array not Hash?

铁路3号申请升级过程中的Im。 我先决定与我的面纱。 有一些遗产,如:

results = ActiveRecord::Base.connection.execute(sql)

使用了2.3.x版本。

results.each_hash do |row|
...

但是,如果是我的sql2,结果为。 Mysql2:Result,它只有each方法。 核对数字,并具体说明结果,应当是用外地名称标明的关键。 页: 1

但事实上,它是一个<代码>Array,而不是一个<编码>Hash。

When I use the rails console and instantiate my own Mysql2::Client and run the query there, the results are a Hash, which is what I want.

在铁路应用中,我认为最好使用<条码>主动记录:Base.linkion,因为它与数据库的选项进行了即时处理。

值得注意的是,不幸的是,结果没有被描绘成一种模式,因此我不得不使用这一模式。

我现在做的是,例如:

result = ActiveRecord::Base.connection.execute(sql)
field_index = result.fields.index("field")
result.each do |row|
  row[field_index]
end

这显然属于罪.祸首。

是否有任何人能够让它回到哈希而不是阿雷拉?

最佳回答

如果你想再使用<代码>数据库.yml。 组合:

config = ActiveRecord::Base.configurations[RAILS_ENV].symbolize_keys
conn = Mysql2::Client.new(config)
conn.query("select * from users").each do |user|
  # user should be a hash
end
问题回答

我面临一个类似的问题,但又认为这可以奏效:

result = ActiveRecord::Base.connection.execute(sql) 
result.each(:as => :hash) do |row| 
   row["field"] 
end

edit: 也可以使用

results = ActiveRecord::Base.connection.select(sql) 

table header

results.first.keys.each do |key|
 key
end

table data

results.each do |result| %>
  result.values.each do |value| %>
    value
  end
end

Improving dan s answer, Rails 3.2.8 won t accept RAILS_ENV.


    config = ActiveRecord::Base.configurations[Rails.env].symbolize_keys
    conn = Mysql2::Client.new(config)
    conn.query("select * from users").each do |user|
        # user should be a hash
    end





相关问题
Ruby net-dns reverse lookups

I am trying to get reverse DNS lookups working with the net-dns gem for ruby. From the rdoc res = Net::DNS::Resolver.new ip = IPAddr.new("172.16.100.2") packet = res.search(ip) packet = res.search("...

How can I show updates with gem?

I want on my webserver a script that checks automatical over cron if there are gem updates without installing updates. I don t find anything about this task in the gem docs.

missing rake tasks?

I have ran gem install rails and am running 2.3.4 but i am missing some rake tasks like db and gems if i run rake -T i get the following tasks. How can i get all the others ? rake apache2 # ...

Ruby Daemons Gem

I installed the ruby gem Daemons. To make sure it was working I created a script that would print to a file every 5 seconds. I then created another file to run the script using the trivial example ...

Ruby gems repository

I m trying to set a gem repository on one of our local servers. Here are the steps I m running, that I ve followed from several guides. 1) I create the BASEDIR folder /var/www/html/gems 2) sudo cp -...

Search logic functionality

The functionality I m looking for: I have a form that will search my Proposal model. I want the form to contain a select box and display the categories I have. Now, category is merely a column in ...

Ruby Geolocation Gem/Plugins [closed]

What are the available (best) ruby IP-based geolocation gem/plugins? How do they compare to one another in terms of functionality, performance and ease of use (e.g. do they interact with a web ...

How do I find gems that depend on a given gem?

Is it possible to search for all gems that rely on a certain rubygem? For example, I d like to ask for all gems in gemcutter that rely on the test-unit gem. Background: I m looking to see how other ...

热门标签