Created
May 13, 2010 18:28
-
-
Save kovyrin/400192 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# не тестировал этот код, просто адаптировал то, что у нас тут уже есть в скрибде | |
# | |
#----------------------------------------------------------------------------------- | |
# | |
# Положить это в initializers/something.rb | |
# | |
module SimpleShardingMethods | |
def db_for_key(key, &block) | |
return on_db(:my_first_db, &block) if key == 1 | |
return on_db(:my_second_db, &block) if key == 2 | |
return on_db(:my_default_db, &block) | |
end | |
end | |
class ActiveRecord::Base | |
extend SimpleShardingMethods | |
include SimpleShardingMethods | |
end | |
ActiveRecord::NamedScope::Scope.send(:include, SimpleShardingMethods) | |
#----------------------------------------------------------------------------------- | |
# | |
# После этого можно будет делать: | |
# | |
MyModel.db_for_key(1).any_ar_method | |
@model_instance.db_for_key(2).save # тут не уверен, но думаю сработает | |
# ну и всякие другие штуки описанные в документации на on_db() :-) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment