2015-11-10 80 views
-4

我是Ruby中的新手。我正在嘗試使用sqlite3。 我有2桌書和用戶,用戶可以有程序中建立的manty書和書屬於用戶。 但是,當我運行mu文件時,出現以下錯誤 Program Files(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 columns' from 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?' from 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 create'ruby​​中的活動記錄錯誤

我讀到這些錯誤,跑耙分貝:遷移它說沒有耙文件中找到 我堅持和需要幫助

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 

「)

在此先感謝

回答

0

你的表被稱爲」用戶「而不是」Libusers「。如果您替換Libuser w ith用戶是否有效?

對於「Libuser」工作,您需要在數據庫中有一個名爲Libusers的表。

小心複數 - 你的ActiveRecord模型應該是單數(用戶,書),你的表名應該是複數(用戶,書籍)。

+0

改變Libuser到用戶,仍然得到錯誤,現在說找不到表「用戶」(的ActiveRecord :: StatementInvalid) –

+0

我剛剛看到您對耙子的評論。你是否在包含你的rake文件的目錄中?因此,在rake文件所在的位置打開終端,然後運行rake db:migrate。要檢查你是否在正確的位置,嘗試rake -T(它會列出所有可能的rake命令) – Valerie

+0

是的,在bin目錄中,我可以看到2個rake文件,1個rake文件和一個windows批處理文件,我運行我的rake數據庫命令在這個文件夾中,但它說沒有找到rake文件。耙 - T讓我耙中止! 找不到Rakefile(查找:rakefile,Rakefile,rakefile.rb,Rakefile.rb) –

0

默認情況下,active_record假定類MyModel(奇異,駝峯)映射到數據庫表my_models(複數,強調),所以Libuser應映射到表libusers,不users

如果你真的想Libuser映射到users(爲什麼?),你可以這樣做:

class Libuser < ActiveRecord::Base 
    self.table_name = 'users' 

    # Other stuff ... 
end