我正在尝试通过使用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
表格映射到患者活动记录模型的尝试失败了:
~/.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进行某种迁移以了解如何将表映射到模型?
答案 0 :(得分:31)
set_table_name
已被删除。试试:
class Patient < ActiveRecord::Base
self.table_name = 'ZPATIENT'
end