活动记录表_是否存在?句法

时间:2011-08-02 15:21:21

标签: mysql ruby activerecord

如何使以下代码生效?我找不到适用于我的语法。

#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

1 个答案:

答案 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?