DRY Rails 模型中的多个相同关联

发布于 2025-01-16 13:33:20 字数 1986 浏览 3 评论 0原文

在我的模型中,我有多个 has_one 关联,就像

has_one  :t1_for_self_order, -> { t1_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :t2_for_self_order, -> { t2_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :minimum_value_for_gifting, -> { minimum_value_for_gifting }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'

您注意到的那样,所有关联中只有具有相同范围名称的关联名称不同,但其余内容是相同的。

所以我想编写一个函数来删除所有这些重复。 我认为元编程是可能的,但我不知道如何做到这一点

此外,我还有一个可以使用的嵌套属性数组。

    THRESHOLD_INTENT = ["t1_for_self_order", "shipping_charges_for_t1", "t2_for_self_order", "shipping_charges_for_t2",
                    "minimum_value_for_gifting", "international_fulfillment_fees", "menu_customization_fee",
                    "total_menu_customization_fee_cap", "video_message_fee", "total_video_message_fee_cap",
                    "swag_price", "box_types_with_price", "customization_box_types_with_price", "custom_note_fee",
                    "non_us_fees"]

我喜欢这样做

THRESHOLD_INTENT.each do |t_intent|
  has_one  t_intent.to_sym, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end

但是我怎样才能通过这样的范围

-> { t1_for_self_order }

In my model, I have multiple has_one associations like

has_one  :t1_for_self_order, -> { t1_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :t2_for_self_order, -> { t2_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :minimum_value_for_gifting, -> { minimum_value_for_gifting }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, as: :source, dependent: :destroy, inverse_of: :source,
          class_name: 'Spree::PriceMapping'

you notice that only the association name with the same scope name is different in all the associations but the rest of the content is same.

So I want to write a function that will remove all these repetitions.
I think it can be possible with Meta programming but I'm not sure how to do this

Moreover, I also have an array of nested attributes which we can use.

    THRESHOLD_INTENT = ["t1_for_self_order", "shipping_charges_for_t1", "t2_for_self_order", "shipping_charges_for_t2",
                    "minimum_value_for_gifting", "international_fulfillment_fees", "menu_customization_fee",
                    "total_menu_customization_fee_cap", "video_message_fee", "total_video_message_fee_cap",
                    "swag_price", "box_types_with_price", "customization_box_types_with_price", "custom_note_fee",
                    "non_us_fees"]

I like to do it in this way

THRESHOLD_INTENT.each do |t_intent|
  has_one  t_intent.to_sym, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end

But how can i pas the scope in this like

-> { t1_for_self_order }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪之触 2025-01-23 13:33:20

您可以迭代关联名称列表,并使用每个项目的所有参数调用 has_one。需要 send(association) 才能将关联范围作为方法调用。

ASSOCIATIONS = [:t1_for_self_order, :shipping_charges_for_t1, :t2_for_self_order, :shipping_charges_for_t2, :minimum_value_for_gifting, :international_fulfillment_fees]

ASSOCIATIONS.each do |association|
  has_one association, -> { send(association) }, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end

You can iterate over a list of association names and call has_one with all the parameters on each item. send(association) is needed to call an association scope as a method.

ASSOCIATIONS = [:t1_for_self_order, :shipping_charges_for_t1, :t2_for_self_order, :shipping_charges_for_t2, :minimum_value_for_gifting, :international_fulfillment_fees]

ASSOCIATIONS.each do |association|
  has_one association, -> { send(association) }, as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping'
end
东北女汉子 2025-01-23 13:33:20

尝试提取公共属性,比如

relationship_attributes = { as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping' }

has_one :t1_for_self_order, -> { t1_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, relationship_attributes
has_one :t2_for_self_order, -> { t2_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, relationship_attributes
has_one :minimum_value_for_gifting, -> { minimum_value_for_gifting }, relationship_attributes
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, relationship_attributes

我不会使用元编程,它会使阅读变得困难

Try extracting the common attributes, something like

relationship_attributes = { as: :source, dependent: :destroy, inverse_of: :source, class_name: 'Spree::PriceMapping' }

has_one :t1_for_self_order, -> { t1_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t1, -> { shipping_charges_for_t1 }, relationship_attributes
has_one :t2_for_self_order, -> { t2_for_self_order }, relationship_attributes
has_one :shipping_charges_for_t2, -> { shipping_charges_for_t2 }, relationship_attributes
has_one :minimum_value_for_gifting, -> { minimum_value_for_gifting }, relationship_attributes
has_one  :international_fulfillment_fees, -> { international_fulfillment_fees }, relationship_attributes

I would not use metaprogramming, it can make it difficult to read

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文