DRY Rails 模型中的多个相同关联
在我的模型中,我有多个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以迭代关联名称列表,并使用每个项目的所有参数调用
has_one
。需要send(association)
才能将关联范围作为方法调用。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.尝试提取公共属性,比如
我不会使用元编程,它会使阅读变得困难
Try extracting the common attributes, something like
I would not use metaprogramming, it can make it difficult to read