`
hideto
  • 浏览: 2667059 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

每天一剂Rails良药之Connecting to Multiple Databases

    博客分类:
  • Ruby
阅读更多
预备知识:
1,Rails启动后没有马上建立数据库连接,而是当model第一次调用connect()方法时建立连接
2,默认情况下ActiveRecord::Base建立数据库连接,然后它的所有子类即所有的model均拥有该连接
3,model查找数据库连接时从自己开始向它的父一层一层查找连接,直到找到为止

如果我们的Rails应用需要建立对多个数据库的连接,我们该怎样做呢?

1,database.yml
development:
  adapter: mysql
  database: default
  username: root
  password:
  socket: /tmp/mysql.sock

products:
  adapter: mysql
  database: products
  username: root
  password:
  socket: /tmp/mysql.sock

这里我们的Rails系统默认使用default这个数据库,products作为外部数据库待使用。

2,product.rb
class Product < ActiveRecord::Base
  establish_connection :products
end

假设products数据库有一个products表,我们用establish_connection来声明建立到哪个数据库的连接即可

3,add_product_reference_table.rb和product_reference.rb
 
class AddProductReferenceTable < ActiveRecord::Migration
  def self.up
    create_table :product_references do |t|
      t.column :product_id, :integer
    end
  end
  def self.down
    drop_table :product_references
  end
end 

class ProductReference < ActiveRecord::Base
  belongs_to :product
  has_and_belongs_to_many :carts,
                          :join_table => "carts_products",
                          :foreign_key => "product_id"
  def name
    product.name
  end
  def price
    product.price
  end 
end	

class Cart < ActiveRecord::Base
  has_and_belongs_to_many :products,
                          :class_name =>  "ProductReference",
                          :join_table => "carts_products",
                          :association_foreign_key => "product_id"

这样我们通过建立一个对product的reference来在我们默认的数据库里直接使用外部数据库的表,不过我们需要同步product_references表和products的id

4,使用外部数据库的多个表时
我们可以建立一个父类,然后集成它即可
class External < ActiveRecord::Base
  self.table_name_prefix = "foo"
  establish_connection :products
end 

class Product < External
end 

class TaxConversion < External
end
分享到:
评论

相关推荐

    Beginning Ruby on Rails

    You'll learn to build Rails applications, connect to databases, perform necessary testing, and put the whole thing together to create real-world applications such as shopping carts and online ...

    Pro Active Record. Databases with Ruby and Rails

    《Pro Active Record: Databases with Ruby and Rails》是一本由Kevin Marshall、Chad Pytel和Jon Yurek共同编写的关于Ruby on Rails(简称Rails)框架下的Active Record模式使用的专业指南。该书于2007年出版,提供...

    Pro ActiveRecord Databases with Ruby and Rails.pdf

    **ActiveRecord**是Ruby on Rails的核心组件之一,它提供了一种简洁的方式来进行数据库交互,通过模型对象(Models)将数据库表映射到程序中。这种方式简化了数据库操作,使得开发人员能够更加专注于业务逻辑而不是...

    Rails 101 入门电子书

    《Rails 101 入门电子书》是一本非常适合初学者直接入门的书籍,它由xdite编写并出版于2014年6月10日。本书主要针对的是希望学习Ruby on Rails框架的读者,特别是那些想要从零开始掌握这项技术的新手。 #### 二、...

    select-multiple-rails:一个小巧的jQuery插件,用于自定义带有multiple属性的selects

    SelectMultiple for Rails [![宝石版本] 是一个小巧的jQuery插件,用于使用multiple属性自定义选择。 select-multiple-rails gem将select-multiple与Rails资产管道集成在一起。 用法 安装select-multi-rails gem ...

    Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf

    综上所述,《Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf》是一个全面的资源,无论你是Rails新手还是资深开发者,都能从中学习到如何为Rails应用编写高质量的测试。从理论到实践,从单元测试...

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

    Apress Pro ActiveRecord Databases with Ruby and Rails.pdf

    **ActiveRecord**是Ruby on Rails框架中的核心组件之一,它实现了ORM的概念,允许开发者以面向对象的方式处理数据库记录。通过ActiveRecord,开发者可以轻松地创建、读取、更新和删除数据库中的数据,而无需编写复杂...

    Rails Recipes英文版(清晰文字pdf+源码)

    Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) Rails is large, powerful, and new. How do you use it effectively? How do you harness the power? And, ...

    Rails之道.pdf(最新版)

    《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入的...

    Rails项目源代码

    Ruby on Rails,通常简称为Rails,是一个基于Ruby编程语言的开源Web应用框架,遵循MVC(Model-View-Controller)架构模式。这个“Rails项目源代码”是一个使用Rails构建的图片分享网站的完整源代码,它揭示了如何...

Global site tag (gtag.js) - Google Analytics