My code is:
# require gems
require 'rubygems'
require 'active_record'
# Start connetion to the server
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:user => "root",
:password => "password",
:database => "data"
)
# Create the table
ActiveRecord::Schema.define do
create_table :datatable do |table|
table.column :date, :string
table.column :word, :string
table.column :website, :string
end
end
class Table < ActiveRecord::Base; end
Table.create(:date => datenow, :word => someword, :website => mywebsite)
puts "code completed"
And I get an error when the code wants to write to the table saying:
path/to/mysql_adapter.rb:287:in 'query': Table 'test.tables' doesn't exist (Mysql::Error)
If I create a table that is called tables within the database (data) then all of my data is put into there. I want to it to be written to the table (datatable) I have just created. How can I do this and solve the error?
The error is to be expected. You’re accessing a table called
table, but creating a table calleddatatable.To configure your
Tablemodel to use thedatatabletable, use ActiveRecord::Base.set_table_name like so:Alternatively, you could rename your model to Datatable.
However, I’d suggest you rename it to something entirely different. Unless you’re actually storing data about tables, your model probably shouldn’t be called
Table.WebsiteWordcomes to mind orWordOccurrenceor perhaps justWord. I have a feeling it’ll save you pain in the long run.