0
我有下面的ruby脚本,用于尝试从我的MySQL数据库检索数据。我正在使用rails 3.2。从MySQL数据库中检索数据
require 'active_record'
require 'mysql2'
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
host: 'localhost',
database: 'financials',
username: 'dbuser',
)
class Financials < ActiveRecord::Base
attr_accessible :symbol, :cur_price....
end
fin = Financials.new
puts fin.find_by symbol: 'arrs'
数据库本身包含以下记录:
symbol cur_price 52low 52high avg_vol
arrs 16.50 11.70 17.98 1062020
当我运行我的脚本,我得到以下错误:
method_missing': undefined method `find_by' for #<Financials:0x007fc54b8ddbd8> (NoMethodError)
我缺少什么?
感谢
感谢您的答复,我使用的轨道3.X和where子句起作用,但它返回#,我如何获取实际内容? –
rahrahruby
我不确定你的内容是什么意思 - 它返回一个Financials对象,你可以调用'fin = Financials.where(symbol:'arrs')。 fin.cur_price'或任何其他列名来获取列内容 –
仅供参考:当你调用'puts fin'时,你实际上正在调用fin的to_s'方法,而对于活动记录模型,在你的注释中产生输出'' –