我尝试使用Active Record连接到一个.sqlite文件并与之交互。我正在进行如下操作:
require 'active_record'
# Connect to DB
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => 'database.sqlite')
# Log the tables to make sure Active Record is connected to the DB correclty
puts ActiveRecord::Base.connection.tables
# Map from existing table records to a Active Record model
class Patient < ActiveRecord::Base
set_table_name "ZPATIENT"
end
连接到数据库后,打印数据库表的输出如下:
ZPATIENT
ZZCONFIG
Z_PRIMARYKEY
Z_METADATA
但是尝试将ZPATIENT
表映射到患者Active Record模型时失败了,具体错误信息如下:
~/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `set_table_name' for Patient(Table doesn't exist):Class (NoMethodError)
from script.rb:8:in `<class:Patient>'
from script.rb:7:in `<main>'
我不确定我做错了什么?我需要进行某种迁移才能让Active Record理解如何将表映射到模型吗?
self.table_name
而不是set_table_name
吗? - nicosantangelo