有段时间没做Rails项目了, 最近准备开始一个新的Rails项目.简单的复习了一下,才发现的确有一些相对少用的特性已经有点遗忘.在此做个记录,也算加深个映像.
Rails中实现单表继承(Single-Table Inheritance)
单表继承这个概念其实是要解决面向对象与关系数据库的一个矛盾或者说不同.在关系数据库中我们并没有继承这个概念.
而单表集成解决这个问题的方式是,将父类和所有子类的属性的并集全部存入一张数据库表中,同时加入一个type字段(Rails默认其为type),来区别此条记录是属于哪一个子类的对象.
下面我举个简单的例子:
create_table :people do |t|
t.string :type
#公共属性
t.string :name
t.string :email
#manager的属性:分红
t.decimal :bonus, :precision => 10, :scale => 2
#developer的属性: 电脑补助
t.decimal :subsidy, :precision => 4, :scale => 2
#customer属性: 地址
t.string :address
t.timestamps
end
model:
#person.rb
class Person < ActiveRecord::Base
end
#employee.rb
class Employee < Person
belongs_to :manager, :class_name => "Employee"
end
#manager.rb
class Manager < Employee
end
#customer.rb
class Customer < Person
end
script/console
>> m = Manager.create(:name => 'Calvin', :email => 'calvin@god.com', :bonus => 100000.00, :subsidy => 200.00)
>> e = Employee.new(:name => 'Terry', :email => 'Terry@god.com',:subsidy => 200.00)
>>e.manager = m
>>c = Customer.create(:name => 'Gates', :email => 'Gates@god.com', :address => 'USA')
此时我们可以看看数据库:
然后我们可以做一些查询.
>> m = Person.find_by_name("Calvin")
=> #<Manager id: 1, type: "Manager", name: "Calvin", email: "calvin@god.com", bonus: #<BigDecimal:23c17c8,'0.1E6',4(12)>, subsidy: #<BigDecimal:23c178c,'0.2E3',4(8)>, manager_id: nil, address: nil, created_at: "2009-09-03 09:47:40", updated_at: "2009-09-03 09:47:40">
>> e = Employee.find_by_name("Terry")
=> #<Employee id: 2, type: "Employee", name: "Terry", email: "Terry@god.com", bonus: nil, subsidy: #<BigDecimal:237fc9c,'0.2E3',4(8)>, manager_id: 1, address: nil, created_at: "2009-09-03 09:55:54", updated_at: "2009-09-03 09:56:24">
>> e.manager
=> #<Manager id: 1, type: "Manager", name: "Calvin", email: "calvin@god.com", bonus: #<BigDecimal:237a418,'0.1E6',4(12)>, subsidy: #<BigDecimal:237a3dc,'0.2E3',4(8)>, manager_id: nil, address: nil, created_at: "2009-09-03 09:47:40", updated_at: "2009-09-03 09:47:40">
最后我们来看看,单表继承的利弊.
好处自不用说,我们用一张表就能存储所有子类的信息.不用为每一个子类创建常见一张表,从数据库的设计角度来讲,可以说是简洁了许多.
但是缺点也很明显每条记录都拥有所有子类的属性,冗余显得比较大.
按DHH的话来说, 这没有一个标准的选择, 只是一种取舍. 他更倾向于用单表继承而已.
关于单表继承更详细的一些阐述也可以参见Martin Fowler的<<企业应用架构模式>>
- 大小: 28.9 KB
分享到:
相关推荐
Chapter 1. Set Up the Environment Chapter 2. Create a Great-Looking Login with Bootstrap and Devise Chapter 3. Secure the User Database with Postgres Constraints Chapter 4. Perform Fast Queries with ...
rails.vim提供了常用的一些命令,可以帮助开发,例如:Rgenerate, Rake, Rfind,RTview等,很方便,也很实用。 安装方法: 拷贝 autoload/rails.vim, plugin/rails.vim, 和 doc/rails.txt 到 ~/.vim 目录. ...
Rails.Recipes.Rails.3.Edition.和源码
Ruby on Rails Bible.pdf Ruby on Rails Bible.pdf
使用RSpec 测试Rails 程序.pdf
This book is intended for intermediate to advanced Rails programmers. It assumes that you already know Ruby, and have either read one of the many wonderful introductory books on Rails or have ...
RailsSpace teaches you Ruby on Rails by developing a real-world application: RailsSpace, a social networking website aimed at the Rails community itself. We take you step by step, from the virtually...
Addison.Wesley.Rails.AntiPatterns.Best.Practice.Ruby.on.Rails.Refactoring
Rails.Angular.Postgres.and.Bootstrap.Powerful.Effective.and.Efficient.Full-Stack.Web.Development.2016.1.pdf
Ruby.on.Rails.Enterprise.Application.Development
Ruby.on.Rails.Tutorial,Learn.Web.Development.with.Rails,Third.Edition-中文版 文字版.pdf 个人收集电子书,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!
[Ruby_on_Rails][中文][Rails_5.x]__03-2_._安裝不同版本的Rails與產生Rails_5.x版
RESTful.Rails.Development.2015.10.pdf
Pragmatic.Security.on.Rails.Dec.2009.rar
This concise teaches you how to use the power of Docker and Kubernetes to deploy your Rails applications easily and efficiently. Docker and Kubernetes are increasing in popularity every day, but what ...
Netbeans 6.0 JRuby on Rails 演示 .swf
Apress,.Beginning.Google.Maps.Applications.with.Rails.and.Ajax.
This short early adopter book details both how you'll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...
Ruby on Rails Guides_ A Guide to Testing Rails Applications.pdf
twitter-bootstrap-rails, 用于 Rails 5 4.x 资产管道的Twitter Bootstrap 用于 Rails 5和 Rails 4资产管道的 Twitter BootstrapBootstrap 是 Twitter的工具包,旨在开发网络应用程序和站点的kickstart 。...