2011-08-02 1 views
2

次のコードを実行するにはどうすればよいですか?私は私のために働く構文を見つけることができません。アクティブレコードtable_exists?構文

#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 

答えて

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? 
関連する問題