如何使以下代码生效?我找不到适用于我的语法。
#Create the table
if table_exists?(tablename)
puts "table found"
else
ActiveRecord::Schema.define do
create_table :data do |table|
table.column :date, :string
table.column :word, :string
table.column :website, :string
end
puts "table created"
end
答案 0 :(得分:4)
一些替代方案:
# Ask from the DB connection
ActiveRecord::Base.connection.table_exists?(:xyz)
# Query tables from Schema
ActiveRecord::Schema.tables.include?("xyz")
# Xyz is your model class, check if its table is present
Xyz.table_exists?