gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:21:02 26 4
gpt4 key购买 nike

我正在尝试通过使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com