我试图设置使用在后端的ActiveRecord来处理所有的数据繁重的一个IRC bot内(可能是矫枉过正,但是这部分是我的一个学习经验:3)使用ActiveRecord另一个类
我正在运行的问题是,在定义我的数据库模式后,当我试图引用我创建的表时,在同一个脚本中,我从SQLite gem中得到一个错误,说它无法找到表。
而且,我的IDE(RubyMine的)抱怨说,它是“无法找到导轨型号:联想笔记场”
有个声音告诉我,如果我不是从作为操作的限制,这将不会发生僵尸框架的类,但这只是一个疯狂的猜测。
我在这里做错了什么?
require 'cinch'
require 'active_record'
puts 'Memobox loaded'
class Memobox
include Cinch::Plugin
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
ActiveRecord::Schema.define do
create_table :notes do |table|
table.column :id, :integer
table.column :timeset, :DateTime
table.column :sender, :string
table.column :recipient, :string
table.column :text, :string
end
end
class Note < ActiveRecord::Base
has_many :notes
end
match(/note.*/, :prefix => "?")
def execute(m)
Memobox::Note.create(
:timeset => (Time.new).ctime,
:sender => m.user.nick,
:text => m.message,
:recipient => (m.message).split("_").at(1)
)
end
end
错误:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/sqlite_adapter.rb:472:in `table_structure': Could not find table 'notes' (ActiveRecord::StatementInvalid)
'Note'有很多'notes'? – oldergod
是的,如果我理解这个权利,Note类就是我要调用的从我的数据库添加和删除笔记的内容,我使用上面创建的笔记表做这件事。班级笔记有很多笔记(数据库行) –
'has_many'和cie。用于声明不同模型之间的关联。不在桌子和班级之间。例如,一个雇主'has_many':员工。这是两个不同表格的两种不同模型。你的情况不需要任何东西。 – oldergod