我正在为位于维基媒体实验室的维基媒体数据库创建一些模型,由于技术问题,有两个表格供修订。
一个表revision
包含所有修订,但缺少关于用户名的索引,因为它是由针对某些修订的空白用户名的表达式组成的视图。
另一个表revision_userindex
有索引,但它缺少受空白影响的修订。
除此之外,它们是相同的,我希望能够将其用作一个单一模型,而不是将索引的低级细节传递给用户。
目前我有以下代码:
class Revision < ActiveRecord::Base
self.table_name = :revision_userindex # or :revision
self.primary_key = :rev_id
has_many :externallinks, :class_name => 'Externallink', :foreign_key => :el_from
has_many :iwlinks, :class_name => 'Iwlink', :foreign_key => :iwl_from
has_many :langlinks, :class_name => 'Langlink', :foreign_key => :ll_from
has_many :pagelinks, :class_name => 'Pagelink', :foreign_key => :pl_from
has_many :recentchanges, :class_name => 'Recentchange', :foreign_key => :rc_this_oldid
belongs_to :page, :class_name => 'Page', :foreign_key => :rev_page
belongs_to :user, :class_name => 'User', :foreign_key => :rev_user
has_many :templatelinks, :class_name => 'Templatelink', :foreign_key => :tl_from
has_many :texts, :class_name => 'Text', :foreign_key => :old_id
end
但是我不知道有什么方法可以self.table_name
根据上下文中是否指定用户来动态设置。IE
Revision.find_by_page("page")
Revision.find(nnn)
Revision.where(...) # where the query doesn't link to the user table
@page.revisions
# etc...
应该使用该revision
表,并且
Revision.find_by_user("user")
@user.revisions
应该使用revision_userindex
表
如果可以在不重新实现一半 AR 的情况下实现这一点,我会很高兴知道。
两个表的主键相同。
作为参考,以下是表格的定义:
首先进行修订:
CREATE ALGORITHM=UNDEFINED DEFINER=`viewmaster`@`%` SQL SECURITY DEFINER VIEW `revision` AS
SELECT `enwiki`.`revision`.`rev_id` AS `rev_id`,
`enwiki`.`revision`.`rev_page` AS `rev_page`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_text_id`) AS `rev_text_id`,
if((`enwiki`.`revision`.`rev_deleted` & 2),NULL,`enwiki`.`revision`.`rev_comment`) AS `rev_comment`,
if((`enwiki`.`revision`.`rev_deleted` & 4),NULL,`enwiki`.`revision`.`rev_user`) AS `rev_user`,
if((`enwiki`.`revision`.`rev_deleted` & 4),NULL,`enwiki`.`revision`.`rev_user_text`) AS `rev_user_text`,
`enwiki`.`revision`.`rev_timestamp` AS `rev_timestamp`,
`enwiki`.`revision`.`rev_minor_edit` AS `rev_minor_edit`,
`enwiki`.`revision`.`rev_deleted` AS `rev_deleted`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_len`) AS `rev_len`,
`enwiki`.`revision`.`rev_parent_id` AS `rev_parent_id`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_sha1`) AS `rev_sha1`
FROM `enwiki`.`revision`
对于修订用户索引:
CREATE ALGORITHM=UNDEFINED DEFINER=`viewmaster`@`%` SQL SECURITY DEFINER VIEW `revision_userindex` AS
SELECT `enwiki`.`revision`.`rev_id` AS `rev_id`,
`enwiki`.`revision`.`rev_page` AS `rev_page`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_text_id`) AS `rev_text_id`,
if((`enwiki`.`revision`.`rev_deleted` & 2),NULL,`enwiki`.`revision`.`rev_comment`) AS `rev_comment`,
`enwiki`.`revision`.`rev_user` AS `rev_user`,
`enwiki`.`revision`.`rev_user_text` AS `rev_user_text`,
`enwiki`.`revision`.`rev_timestamp` AS `rev_timestamp`,
`enwiki`.`revision`.`rev_minor_edit` AS `rev_minor_edit`,
`enwiki`.`revision`.`rev_deleted` AS `rev_deleted`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_len`) AS `rev_len`,
`enwiki`.`revision`.`rev_parent_id` AS `rev_parent_id`,
if((`enwiki`.`revision`.`rev_deleted` & 1),NULL,`enwiki`.`revision`.`rev_sha1`) AS `rev_sha1`
FROM `enwiki`.`revision`
WHERE ((`enwiki`.`revision`.`rev_deleted` & 4) = 0)