ruby - .sqlite 文件的 Active Record set_table_name 错误

标签 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

与数据库的连接就像打印出数据库表一样:

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 进行某种迁移以了解如何将表映射到模型?

最佳答案

set_table_name 已被删除。尝试:

class Patient < ActiveRecord::Base
  self.table_name = 'ZPATIENT'
end

关于ruby - .sqlite 文件的 Active Record set_table_name 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17498147/

相关文章:

python - 如何在 Python 中执行此 Ruby if-with-and 并且不会使列表索引超出范围

Android 内容 uri id 作为字符串

mysql - 为什么使用 ActiveRecord 而不是 MySql API

xcode - 在使用 sqlite、fmdb 和 swift 时遇到问题

c# - SQLite 数据库 (.db) 的性能问题

php - 获取在两个日期之间发布的数据

ruby-on-rails - rails 协会 :autosave doesn't seem to working as expected

javascript - 是否有 JavaScript 的 Function.prototype.bind 的 Ruby 等价物?

ruby-on-rails - 如何将 JSON 文件中的字符串转换为 Ruby 哈希?

ruby - 将 JS 文件注入(inject) capybara chrome headless