Upgrade to Pro — share decks privately, control downloads, hide ads and more …

複数DBとRails

 複数DBとRails

Rails複数DB Casual Talksで使ったスライドです
http://connpass.com/event/9560/

Kohei Suzuki

November 10, 2014
Tweet

More Decks by Kohei Suzuki

Other Decks in Programming

Transcript

  1. SXTQMJUUJOH !13 class SummaryBase < ActiveRecord::Base self.abstract_class = true establish_connection

    :"#{Rails.env}_summary" ! def self.with_master establish_connection :"#{Rails.env} _summary_writable" yield ensure establish_connection :"#{Rails.env}_summary" end end ! ! !
  2. SXTQMJUUJOH !15 SwitchPoint.configure do |config| config.define_switch_point :main, readonly: :"#{Rails.env}_slave" !

    config.define_switch_point :bargain, readonly: :"#{Rails.env}_bargain_slave", writable: :"#{Rails.env}_bargain" ! … end
  3. SXTQMJUUJOH !16 class MainBase < ActiveRecord::Base self.abstract_class = true use_switch_point

    :main end ! class BargainBase < ActiveRecord::Base self.abstract_class = true use_switch_point :bargain end ! class User < MainBase; end class Recipe < MainBase; end ! class Bargain::Shop < BargainBase; end class Bargain::Leaflet < BargainBase; end
  4. %#Λލ͙+0*/ !20 class A < ActiveRecord::Base end ! class B

    < ActiveRecord::Base belongs_to :a end irb(main):001:0> B.includes(:a).pluck(:id) (0.2ms) SELECT `bs`.`id` FROM `bs` LEFT OUTER JOIN `as` ON `as`.`id` = `bs`.`a_id`
  5. %#Λލ͙+0*/ !21 class A < ActiveRecord::Base end ! class B

    < ActiveRecord::Base belongs_to :a end irb(main):001:0> B.preload(:a).pluck(:id) (0.2ms) SELECT `bs`.`id` FROM `bs`
  6. EBUBCBTF@SFXJOEFS EBUBCBTF@DMFBOFSʹ୅ΘΔHFN !BNBUTVEB͞Μ͕࡞ͬͨ This strategy was originally devised and implemented

    by Shingo Morita (@eudoxa) at COOKPAD Inc. IUUQTHJUIVCDPNBNBUTVEBEBUBCBTF@SFXJOEFSDSFEJU !22