9

我正在尝试通过使用 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表映射到 Patient 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 进行某种迁移以了解如何将表映射到模型?

4

1 回答 1

32

set_table_name去掉了。尝试:

class Patient < ActiveRecord::Base
  self.table_name = 'ZPATIENT'
end
于 2013-07-05T23:57:46.557 回答