Skip to content

Instantly share code, notes, and snippets.

@adamgamble
Created August 29, 2012 15:54
Show Gist options
  • Save adamgamble/3514777 to your computer and use it in GitHub Desktop.
Save adamgamble/3514777 to your computer and use it in GitHub Desktop.
class MainTable < ActiveRecord::Base
def self.migrate_from_temp_table
ActiveRecord::Base.transaction do
ActiveRecord::Base.connection.execute("DROP TABLE backup_tables")
ActiveRecord::Base.connection.execute("SELECT * INTO backup_tables FROM main_tables")
ActiveRecord::Base.connection.execute("DROP TABLE main_tables")
#raise ActiveRecord::Rollback
ActiveRecord::Base.connection.execute("SELECT * INTO main_tables FROM temp_tables")
TempTable.destroy_all
end
end
end
That works fine, until I get out of the console and back in. Then rails forgets how to do ordering
1.9.3p194 :001 > MainTable.last
MainTable Load (0.3ms) SELECT "main_tables".* FROM "main_tables" ORDER BY "main_tables"."" DESC LIMIT 1
PG::Error: ERROR: zero-length delimited identifier at or near """"
LINE 1: ...les".* FROM "main_tables" ORDER BY "main_tables"."" DESC LI...
^
: SELECT "main_tables".* FROM "main_tables" ORDER BY "main_tables"."" DESC LIMIT 1
ActiveRecord::StatementInvalid: PG::Error: ERROR: zero-length delimited identifier at or near """"
LINE 1: ...les".* FROM "main_tables" ORDER BY "main_tables"."" DESC LI...
^
: SELECT "main_tables".* FROM "main_tables" ORDER BY "main_tables"."" DESC LIMIT 1
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1158:in `async_exec'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1158:in `exec_no_cache'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:664:in `block in exec_query'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:663:in `exec_query'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1253:in `select'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/querying.rb:38:in `block in find_by_sql'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/explain.rb:40:in `logging_query_plan'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/querying.rb:37:in `find_by_sql'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/relation.rb:171:in `exec_queries'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/relation.rb:160:in `block in to_a'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/explain.rb:33:in `logging_query_plan'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/relation.rb:159:in `to_a'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:390:in `find_last'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/relation/finder_methods.rb:146:in `last'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/activerecord-3.2.8/lib/active_record/querying.rb:5:in `last'
from (irb):1
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /Users/adamgamble/.rvm/gems/ruby-1.9.3-p194@test_pg_app/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment