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'方法,而對於活動記錄模型,在你的註釋中產生輸出'' –