ruby on rails - How can I make a model belong_to two times the same model? - Stack Overflow

I have a model Entity that can have offers and invoices. The user can create custom mumber formats to d

I have a model Entity that can have offers and invoices. The user can create custom mumber formats to display the invoice number. For example the internal invoice number 23 could become 2025INV00000023 or something like that. Now, for offers, we need to be able to use a different number format. 23 there could become 2025OFF00000023.

I have done this by creating a NumberFormat class where the user can configure number formats in several ways. Number formats are attached to entities because all invoices from an entity need to have uniform/consistent numbering for tax/accounting purposes.

My Entity model starts like this:

class Entity < ApplicationRecord
  has_many :invoices, dependent: :restrict_with_error
  has_many :offers, dependent: :restrict_with_error

some more unrelated has_many

  belongs_to :invoice_number_format, class_name: 'NumberFormat'
  belongs_to :offer_number_format, class_name: 'NumberFormat'

Now my NumberFormat class needs to have the other end of the belongs_to relationship. But I'm not sure if the doubling there is the correct approach in order to have a dependent: :restrict_with_error preventing the user from deleting number formats that are still in use. VSCode's autoformatter just deletes the second one on save. I can of course save without formatting (and take great care to not fet whenever editing this file) and after preliminary testing it seems like it works but I'm wondering if my approach is maybe not ideal?

  has_many :entities, inverse_of: :invoice_number_format,
                      dependent: :restrict_with_error
  # This line gets deleted on save
  has_many :entities, inverse_of: :offer_number_format,
                      dependent: :restrict_with_error

I have a model Entity that can have offers and invoices. The user can create custom mumber formats to display the invoice number. For example the internal invoice number 23 could become 2025INV00000023 or something like that. Now, for offers, we need to be able to use a different number format. 23 there could become 2025OFF00000023.

I have done this by creating a NumberFormat class where the user can configure number formats in several ways. Number formats are attached to entities because all invoices from an entity need to have uniform/consistent numbering for tax/accounting purposes.

My Entity model starts like this:

class Entity < ApplicationRecord
  has_many :invoices, dependent: :restrict_with_error
  has_many :offers, dependent: :restrict_with_error

some more unrelated has_many

  belongs_to :invoice_number_format, class_name: 'NumberFormat'
  belongs_to :offer_number_format, class_name: 'NumberFormat'

Now my NumberFormat class needs to have the other end of the belongs_to relationship. But I'm not sure if the doubling there is the correct approach in order to have a dependent: :restrict_with_error preventing the user from deleting number formats that are still in use. VSCode's autoformatter just deletes the second one on save. I can of course save without formatting (and take great care to not fet whenever editing this file) and after preliminary testing it seems like it works but I'm wondering if my approach is maybe not ideal?

  has_many :entities, inverse_of: :invoice_number_format,
                      dependent: :restrict_with_error
  # This line gets deleted on save
  has_many :entities, inverse_of: :offer_number_format,
                      dependent: :restrict_with_error
Share Improve this question asked Mar 9 at 6:05 Ricky883249Ricky883249 291 silver badge6 bronze badges 2
  • I just found out that if I disable the DuplicateAssociation RuboCop this also prevents the autoformatter from deleting the line. The question about the correctness of the pattern still stands. – Ricky883249 Commented Mar 9 at 6:15
  • 1 Hard to say much without having more details, but on a first glance, it doesn't feel like there is a need for NumberFormat to be an active record class. It feels like number should just be an attribute on Entity with some validation rules depending on the format. I might be missing a lot of context though. – lmtaq Commented Mar 9 at 10:46
Add a comment  | 

1 Answer 1

Reset to default 1

You can have any number of associations pointing to the same model but each association needs to have a unique name or you will just overwrite the previous definition and it's methods.

class Entity < ApplicationRecord
  belongs_to :invoice_number_format, 
    class_name: 'NumberFormat'
    inverse_of: :entities_as_invoice_number_format
  belongs_to :offer_number_format, 
    class_name: 'NumberFormat
    inverse_of: :entities_as_offer_number_format
end

class NumberFormat < ApplicationRecord
  has_many :entities_as_invoice_number_format, 
    inverse_of: :invoice_number_format,
    dependent: :restrict_with_error,
    foreign_key: :invoice_number_format_id,
    class_name: 'Entity'
  has_many :entities_as_offer_number_format, 
    inverse_of: :offer_number_format,
    dependent: :restrict_with_error,
    foreign_key: :offer_number_format_id,
    class_name: 'Entity'
end

The foreign_key and class_name options are required here as they are derived from the name of the assocation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744876792a4598594.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信