百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

acts_as_relation

> 编程语言
开源

rails 中的多表继承

180 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

rails 中的多表继承

{}[http://badge.fury.io/rb/acts_as_relation] {}[https://travis-ci.org/hzamani/acts_as_relation] {}[https://coveralls.io/r/hzamani/acts_as_relation?branch=master] {}[https://codeclimate.com/github/hzamani/acts_as_relation]

= Deprication This gem is depricated, please use {active_record-acts_as}(https://github.com/hzamani/active_record-acts_as) which is a full refactor of this one.

= ActsAsRelation This gem helps implement multiple-table-inheritance (MTI) methods to your ActiveRecord models. By default, ActiveRecord only supports single-table inheritance (STI). MTI gives you the benefits of STI but without having to place dozens of empty fields into a single table.

Take a traditional e-commerce application for example... A product has common attributes (+name+, +price+, +image+ ...), while each type of product has its own attributes... +pen+ has +color+, +book+ has +author+ and +publisher+ and so on.

== Installation For Rails 4 installation add the following line to your +Gemfile+ gem 'acts_as_relation', '~> 1.0' and $ bundle install

If you are using Rails 3 you must use '~> 0.1' version specifier in Gemfile.

== Usage +acts_as_relation+ uses a polymorphic +has_one+ association to simulate multiple-table inheritance. For the e-commerce example you would declare the product as a +supermodel+ and all types of it as +acts_as+ +:product+ (if you prefer you can use their aliases +is_a+ and +is_a_superclass+)

class Product true do |t| # ... end

Or declare them as you do on a +polymorphic+ +belongs_to+ association, it this case you must pass name to +acts_as+ and +acts_as_superclass+ in +:as+ option:

change_table :products do |t| t.integer :producible_id t.string :producible_type end

class Product :producible end

class Pen :producible end

class Book :producible end

Now +Pen+ and +Book+ act as +Product+. This means that they inherit +Product+ attributes, associations, validations and methods.

To see its functionality lets add some stuff to product:

class Product validates_presence_of :name, :price

def to_s
  "#{name} $#{price}"
end

end

now we can to things like this:

Pen.create :name => "Nice Pen", :price => 1.3, :color => "Red" Pen.where "name = ?", "Some Pen" pen = Pen.new pen.valid? # => false pen.errors.keys # => [:name, :price] Pen.first.to_s # => "Nice Pen $1.3"

When you declare an +acts_as+ relation, the declaring class automatically gains parent methods (includeing accessors) so you can access them directly.

On the other hand you can always access a specific object from its parent by calling +specific+ method on it:

Product.first.specific # will return a specific product, a pen for example

In +has_many+ case you can use subclasses:

store = Store.create store.products

== Options The +acts_as+ relation support these options:

  • +:as+
  • +:auto_join+
  • +:class_name+
  • +:dependent+

when +:auto_join+ option set to +true+ (which is by default), every query on child will automatically joins the parent table. For example:

Pen.where("name = ?", "somename")

will result in the following SQL:

SELECT "pens".* FROM "pens" INNER JOIN "products" ON "products"."as_product_id" = "pens"."id" AND "products"."as_product_type" = 'Pen' WHERE (name = 'somename')

All other options are same as +has_one+ options.

Note that support for +:conditions+ and +:include+ has been removed. In their place, you can use +where()+ and +includes()+:

acts_as :product, -> { where(color: "yellow") }
acts_as :person, -> { includes(:friends) }

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言