-4

i am a novice in ruby. i am trying my hands on sqlite3. i have 2 tables books and users, users can have manty books and books belong to user which is established in program. however i get following errors when i run mu file 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:incolumns' 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:incolumns' from 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:insubclass_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:increate'

i read about these error and ran rake db:migrate it says no rake file found i am stuck and need help

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

")

thanks in advance

2 Answers2

0

Your table is called "Users" rather than "Libusers". If you replace Libuser with User does it work?

For "Libuser" to work you would need a table called Libusers in your database.

Be careful of plurals - your ActiveRecord models should be singular (User, Book) and your table names should be plural (Users, Books).

Valerie
  • 219
  • 2
  • 13
  • changed Libuser to User, still getting error, now says not find table 'users' (ActiveRecord::StatementInvalid) – Meghana Ravikumar Nov 10 '15 at 15:57
  • I just saw your comment about rake. Are you inside the directory that includes your rake file? So open a terminal in the location where the rake file is located then run rake db:migrate. To check if you are in the right location, try rake -T (it will list all the possible rake commands) – Valerie Nov 10 '15 at 16:07
  • yes, in the bin directory, i can see 2 rake files, 1 rake file and one windows batch file, i ran my rake db command in this folder but it says rake file not found . rake -T gives me rake aborted! No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb) – Meghana Ravikumar Nov 10 '15 at 16:21
0

By default, active_record assumes that the class MyModel (singular, camelcase) maps to the database table my_models (plural, underscored), so Libuser should map to table libusers, not users.

If you really want Libuser to map to users (why?), you can do this:

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

  # Other stuff ...
end
Aetherus
  • 8,720
  • 1
  • 22
  • 36