我是红宝石的新手。我正在尝试使用sqlite3。
我有2个表书和用户,用户可以拥有属于用户的manty书和书籍,这是在程序中建立的。
但是当我运行mu文件时,我得到以下错误
程序文件(x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-active_record/connection_adapters/sqlite3_adapter.rb:511:in table_structure': Could not find table 'libusers' (ActiveRecord::StatementInvalid)
from C:/Program Files (x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:385:in
列'
来自C:/ Program Files(x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:43:in columns'
from C:/Program Files (x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/attributes.rb:93:in
列'
从C:/ Program Files(x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/attributes.rb:98:in columns_hash'
from C:/Program Files (x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:205:in
subclass_from_attributes? '
从C:/ Program Files(x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:54:in new'
from C:/Program Files (x86)/Ruby200-x64/lib/ruby/gems/2.0.0/gems/activerecord-4.2.4/lib/active_record/persistence.rb:33:in
创建&# 39;
我读到了这些错误并运行了rake db:migrate它说没有找到rake文件 我被困住了,需要帮助
require 'active_record'
require 'sqlite3'
`enter code here`ActiveRecord::Base.establish_connection(:adapter => "sqlite3",:database => "memory")
class Clean < ActiveRecord::Migration
def self.up
#ActiveRecord::Schema.define do
create_table :users do |table|
table.column :name, :string
table.column :age, :integer
table.column :books_borrowed,:integer
end
create_table :books do |table|
table.column :borrower, :string
table.column :title, :string
table.column :borrow_date, :string
table.column :due_date, :string
end
end
def self.down
drop_table :users
drop_table :books
end
end
class Libuser < ActiveRecord::Base
has_many :books
end
class Books< ActiveRecord::Base
belongs_to :user
end
#Clean.down
Clean.up
user= Libuser.create(:name=>"Megna", :age=>25, :books_borrowed=>2 )
user.books.create(:title=>"immortals of meluha", :borrow_date=>"12 jan 2015", :due_date=>"22 jan 2015")
user.books.create(:title=>"secret of nagas", :borrow_date=>"24 jan 2015", :due_date=>"2 Feb 2015")
user= Libuser.create(:name=>"sandhya", :age=>27, :books_borrowed=>3 )
user.books.create(:title=>"ugly duckling ", :borrow_date=>"12 feb 2015", :due_date=>"22 feb 2015")
user.books.create(:title=>"Little red riding Hood", :borrow_date=>"24 march 2015", :due_date=>"2 april 2015")
user.books.create(:title=>"Little red riding Hood", :borrow_date=>"12 april 2015", :due_date=>"22 april 2015
&#34)
提前致谢
答案 0 :(得分:0)
您的桌子被称为&#34;用户&#34;而不是&#34; Libusers&#34;。如果用User替换Libuser它是否有效?
对于&#34; Libuser&#34;要工作,你需要在数据库中使用一个名为Libusers的表。
注意复数 - 您的ActiveRecord模型应该是单数(用户,书籍),并且您的表名应该是复数(用户,书籍)。
答案 1 :(得分:0)
默认情况下,active_record假定类MyModel
(单数,camelcase)映射到数据库表my_models
(复数,下划线),因此Libuser
应映射到表{{1 }},而不是libusers
。
如果您真的希望users
映射到Libuser
(为什么?),您可以这样做:
users