3
我使用activerecord連接mysql數據庫並在連接後創建一個表。 到目前爲止它效果很好,問題是我不知道如何檢查表是否已經存在。我認爲這將適用於table.exist?但不知何故,我沒有... ... 所以這是我走到這一步:檢查mysql表是否已經存在
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