高级 Active Record 技术深度解析
1. STI 与关联关系
在应用程序,尤其是数据管理类应用中,经常会遇到一些模型,它们的数据负载非常相似,但行为和相互之间的关联有所不同。以处理州、县、市和社区人口信息的 Rails 应用为例,这些都属于地点,可以定义一个单表继承(STI)类 Place 。
以下是 places 数据库表的原始架构和 Place 类的代码:
# == Schema Information
#
# Table name: places
#
# id :integer(11) not null, primary key
# region_id :integer(11)
# type :string(255)
# name :string(255)
# description :string(255)
# latitude :decimal(20, 1)
# longitude :decimal(20, 1)
# population :integer(11)
# created_at :datetime
# updated_at :datetime
class Place < ActiveRecord::Base
end
Place 本质上是一个抽象类,在 Ruby 中没有绝对可靠的方法来强制它不被实例化。接下来定义
超级会员免费看
订阅专栏 解锁全文
4

被折叠的 条评论
为什么被折叠?



