![](/img/trans.png)
[英]Validate HAML from ActiveRecord: scope/controller/helpers for link_to etc?
[英]Rewrite scope to use ActiveRecord helpers
我的模型具有以下作用域:
scope :by_attributes, ->(names) {
attribute_ids = ReferralPartnerAttribute.where(name: names).pluck(:id)
if attribute_ids.any?
where(
"ARRAY[?] <@ (
SELECT array_agg(referral_partner_attribute_id)
FROM referral_partner_referral_partner_attributes
WHERE referral_partner_referral_partner_attributes.referral_partner_id = referral_partners.id)", attribute_ids
)
else
none
end
}
如何以更ActiveRecor的方式重寫它? 這可能嗎?
在范圍內調用另一個范圍如何?
scope :by_attributes, ->(names) { (attribute_ids = ReferralPartnerAttribute.where(name: names).pluck(:id)).present? ? another_scope(attribute_ids) : none }
scope :another_scope, ->(attribute_ids) { where("ARRAY[?] <@ (
SELECT array_agg(referral_partner_attribute_id)
FROM referral_partner_referral_partner_attributes
WHERE referral_partner_referral_partner_attributes.referral_partner_id = referral_partners.id)", attribute_ids
)}
如果您已經有attribute_ids
,並且不需要檢查它是否為空,那么這樣做也將有助於直接調用此作用域。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.