Rails 关系型数据库继承遇到的问题

jky · 2014年05月27日 · 最后由 Peter 回复于 2014年05月27日 · 2304 次阅读

继承时 new 怎么只有 superclass 的属性而本身的属性怎么没有呢?用的是 mysql 数据库。请大神帮忙指教需要注意什么问题

也许你需要看看 Rails Guide 里的 polymorphic 也许你需要

类似的东西

#1 楼 @5swords acts_as_citier 是要安宝石 gem 'acts_as_relation', '~> 1.0'是吧

#1 楼 @5swords acts_as_citier 是要安宝石 gem 'acts_as_relation', '~> 1.0'是吧

#1 楼 @5swords acts_as_citier 是要安宝石 gem 'acts_as_relation', '~> 1.0'是吧

#2 楼 @jky 不是 gem "rails_sql_views", git: "git://github.com/TheHiveProjects/rails_sql_views.git" gem "citier", git: "git://github.com/sishen/citier.git"

不过,这个 citier 很久没人维护了,上面是 yedingding 的版本,我用在 rails 3.2.14,不知道 rails4 怎么样。 意思就是可能有大坑。现在 2 层的继承没问题,3 层的就有问题。一旦用了,你要有自己改源码的觉悟。

acts_as_relation 我自己没用过

总共只有一张表,继承来继承去还是那张表上面的数据,能有什么问题?

#6 楼 @billy 不是单表继承,是用 view 来做子类, 如 Party < Group < Department,Party 一张表,Group 有一张表 groups 有一个 view_groups,Department 有一张表 departments 一个 view_departments

对 Department 的读从 view_departments 来,写要分配到 3 张表里去,删除要一起删,id 是统一的,还有索引的问题。

#6 楼 @billy 继承是一张表么?不是一张表吧

没有代码是怎么讨论的……

@5swords 还不清楚楼主的具体情况,他说得很模糊。

@jky

Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::Base, then Message is used to guess the table name even when called on Reply.

http://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name

你如果没有自己写 table_name,那肯定就是一张表。

如果 column 大量重复,你当然是用一张表罗,常用的做法是设置一个 type, 就是 5s 说的 porlymorphic。

如果 column 很少重复的,那。。。那真的没有必要继承。Use composition or duck typing。

@Rei, 我亦觉得@5swords联想过于丰富。

不知道楼主想做什么,是不是这个问题? How can i use multiple controller with one model? http://stackoverflow.com/questions/11929693/how-can-i-use-multiple-controller-with-one-model

需要 登录 后方可回复, 如果你还没有账号请 注册新账号