Skip to content

Instantly share code, notes, and snippets.

View brandon-beacher's full-sized avatar
💭
🦑

Brandon Beacher brandon-beacher

💭
🦑
View GitHub Profile
if Rails.env.production?
Sass::Plugin.options.merge!(
:template_location => 'public/stylesheets/sass',
:css_location => 'tmp/stylesheets'
)
Rails.configuration.middleware.delete('Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Sass::Plugin::Rack')
Rails.configuration.middleware.insert_before('Rack::Sendfile', 'Rack::Static',
class AddUserIdToJobs < ActiveRecord::Migration
def change
add_column :jobs, :user_id, :integer
end
end
def charity_donations_total_amount
charity_donations.reduce(0) do |sum, charity_donation|
sum + charity_donation.total_amount
end
end
# versus
def charity_donations_total_amount
sum = 0
actionmailer (2.3.11)
actionpack (2.3.11)
activemodel (3.0.7)
activerecord (2.3.11)
activeresource (2.3.11)
activesupport (3.0.7, 2.3.11)
acts_as_audited (1.1.1)
addressable (2.2.6)
arel (2.0.10)
Ascii85 (1.0.1)
maximum_id = 2
[:customer_a, :customer_b].each_with_index do |database_name, index|
id_offset = index * maximum_id
ActiveRecord::Base.connection.execute %{
insert into people
select
`id` + #{id_offset},
maximum_id = 0
[:customer_a, :customer_b].each do |database_name|
ActiveRecord::Base.establish_connection(database_name)
maximum_id_in_database = 0
ActiveRecord::Base.connection.tables.each do |table_name|

Here at Highgroove we like a good challenge, and we love it when we can meet a challenge with a simple and elegant solution.

Recently we had a customer with a great problem to have - they had signed up lots of new customers - but were experiencing some growing pains.

You see - a typical Ruby on Rails app has one database. This customer's app had one database per customer.

Routine tasks like launching servers, deploying code, or migrating databases were taking much longer than normal because they had to be performed per customer instead of just once.

What this app needed instead was Multitenancy - a fancy word for combining the customers into one database.

class Job < ActiveRecord::Base
serialize :data
serialize :exception
named_scope :current_customer, lambda { { :conditions => { :customer_id => Customer.current.id } } }
def self.spawn(person_id, data)
customer = Customer.current
job = create!(:person_id => person_id, :data => data)
job.spawn do
➜ giving101 git:(master) git push heroku master
Counting objects: 19, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (10/10), 1.16 KiB, done.
Total 10 (delta 6), reused 0 (delta 0)
-----> Heroku receiving push
-----> Ruby/Rails app detected
-----> Detected Rails is not set to serve static_assets