Skip to content

Instantly share code, notes, and snippets.

@NickClark
NickClark / Question.markdown
Created March 8, 2011 15:20
Importing Multiple databases

Hey all,

I am writing a rake script to import data from an old php database, into my shiny new rails app. In the script I define a few temp ActiveRecord classes to represent the tables from the old system as shown in the excerpt below. This works fine with one legacy database. The problem is that I have many databases with the same tables but some those tables columns differ. My code takes that into account appropriatly. The problem is that when I change the underlying database being used to the next one, then the models seem to not update properly, any idea on a better way to do this?

@NickClark
NickClark / role.rb
Created February 21, 2011 01:06
Is there not a better way?
# So i want to have quick access to some standard roles in my Roles table
# The following works, but I am regenerating my table for scratch, and I run
# rake db:schema:load, then I get errors because rails loads all the code first,
# and when it gets to these class methods, it errors out since it cant find the
# roles table yet. Which is exactly what I am trying to do!! Any Ideas how I can improve this?
Class Role < ActiveRecord::Base
def self.admin
find_by_name "Administrator"
puts caller
puts 'Included file'
shared_examples_for 'All Admin Controllers' do
...
end
@NickClark
NickClark / my_controller.rb
Created December 1, 2010 15:47
skip_before_filter(filter_chain) is used in older rails 2 app. What would the rails 3 equiv be?
class MyController < ActionController::Base
#What is the rails 3 equivilent?
skip_before_filter filter_chain
end
@NickClark
NickClark / legacy_import.rake
Created October 22, 2010 19:46
legacy_import.rake
namespace :app_name do
desc "Import data from old database"
task :legacy_data_import => :environment do
class LegacyDB < ActiveRecord::Base
self.abstract_class = true
establish_connection(:legacy)
end
#check for column named 'class', this will conflict with Rails,
#so we will change the column name
notice: HEAD points to an unborn branch (master)
notice: No default references
dangling tree 41cdfb0c468502b1caae57a691e23d931ba1e0f0
dangling tree eb59b3e81fd80e521708fcc0137ebcd01c0a9082
I would like to convert this:
RewriteCond %{REQUEST_URI} !forms(.*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php
Into this:
if ($request_uri !~* forms(.*) && !-f @request_filename && !-d @request_filename ) {
server {
listen 80 default;
listen 443 default ssl;
server_name www.example.com;
if ($server_port = 443) {
proxy_set_header X_FORWARDED_PROTO https;
}
}
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$('dl.update dd').hide();
$('dl.update dt').toggle(
function(e){
if(e.originalTarget === this){
$('form').live('submit', function (e) {
var data = $(this).serializeArray();
$.ajax({
url: "http://www.example.com/",
data: data,
dataType: "script",
type: "POST",
});
e.preventDefault();
});