luk*_*r90 9 ruby sqlite activerecord ruby-on-rails-4
我正在尝试通过使用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
Run Code Online (Sandbox Code Playgroud)
与数据库的连接在打印出数据库表时起作用:
ZPATIENT
ZZCONFIG
Z_PRIMARYKEY
Z_METADATA
Run Code Online (Sandbox Code Playgroud)
但是将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>'
Run Code Online (Sandbox Code Playgroud)
不确定我做错了什么?我是否需要为Active Record进行某种迁移以了解如何将表映射到模型?
alf*_*alf 31
set_table_name去掉了.试试:
class Patient < ActiveRecord::Base
self.table_name = 'ZPATIENT'
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4275 次 |
| 最近记录: |