这是通常的AREL或Sequel基于宝石的模型的“开箱即用”,所以我正在寻找一些专家指导。
我的app之外有一个遗留(事务)表,每次部署时都会更新。 Deployments
看起来像这样:
DEPLOY_ID | APP_DEPLOY_NAME | DEPLOY_TYPE | DOMAIN | DEPLOY_ENV | ...
1 | adn | dta | ruby | dev1 | ...
2 | adn | dta | ruby | dev1 | ...
* 3 | adn | dta | ruby | dev1 | ...
* 4 | adn | dta | ruby | dev2 | ...
* 5 | adnb | dta | ruby | dev1 | ...
上面标记的行代表每个唯一DEPLOY_ID
条目的最高APP_DEPLOY_NAME + DEPLOY_TYPE + DOMAIN + DEPLOY_ENV
。从概念上讲,这些是由这四个值定义的复合键的最新部署。
在SQL中,我可以轻松地使用自联接创建此视图:
select deployments.* from
-- deploy_max
(select max(DEPLOY_ID) as DEPLOY_ID from DEPLOYMENTS
group by APP_DEPLOY_NAME, DEPLOY_TYPE, DOMAIN, DEPLOY_ENV) deploy_max
join DEPLOYMENTS as deployments on
deploy_max.DEPLOY_ID = deployments.DEPLOY_ID
order by deployments.APP_DEPLOY_NAME asc,
deployments.DEPLOY_TYPE asc,
deployments.DOMAIN asc
(这可能是一种更好的写作方式,但它显示了我正在尝试做的事情)
但是,这会失去查询链接的好处...即我似乎无法获得类似:latest.filter(:APP_DEPLOY_NAME.like('%adn%'))
的内容。
其他皱纹:
有什么想法吗?
答案 0 :(得分:1)
以下是您的SQL查询到Sequel代码的直接翻译:
DB[:deployments].
select{max(deploy_id).as(deploy_id)}.
group(:app_deploy_name, :deploy_type, :domain, :deploy_env).
from_self(:alias=>:deploy_max).
join(:deployments, :deploy_id=>:deploy_id).
select_all(:deployments).
order(:deployments__app_deploy_name, :deployments__deploy_type, :deployments__domain)