I use activerecord to connect to a mysql database and to create a table after the connection. So far it works great, the problem is that I don't know how to check if the table already exist. I thought it would work with table.exist? but somehow I didn't... ...So this is what I got so far:
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "my-username",
:password => "my-password",
:database => "my-db",
:encoding => "UTF8"
)
# How to check if it exists already? table_name.table.exist? doesnt really work...
name = "my_table"
if name.!table.exist?
ActiveRecord::Schema.define do
create_table :"#{name}" do |table|
table.column :foo, :string
table.column :bar, :string
end
end
else
puts "Table exist already..."
end
# Create ActiveRecord object for the mysql table
class Table < ActiveRecord::Base
set_table_name "#{name}"
end