ruby - In Rails model, can we not save an item if the table doesn't exist? -


i have several databases use records model. in model's after_save callback, creating new row in logtable. have like:

class records < activerecord::base   after_save :record_to_log   def record_to_log     new_log = logtable.new     new_log.keyword = "keyword"     new_log.save   end end 

but not databases have logtable table. getting error says logtable doesn't exist. how prevent error, , there way abort save if table doesn't exist in particular database?

you can check if table exists by

activerecord::base.connection.table_exists? 'log_table' 

the callback can modified like

after_save :record_to_log, :if => activerecord::base.connection.table_exists?('log_table') 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -