开发者

ActiveRecord: when is it polymorphic

开发者 https://www.devze.com 2023-02-05 02:08 出处:网络
I\'ve always found the documentation around the polymorphic association unclear.For example, from The Rails Guide:

I've always found the documentation around the polymorphic association unclear. For example, from The Rails Guide:

开发者_StackOverflow社区

With polymorphic associations, a model can belong to more than one other model, on a single association.

I find I have many cases where a model belongs_to more than one other model. For example if I have a Company model, an Invoice model and a AccountPayable model, I've found I can simply do:

class Company < ActiveRecord::Base
   has_many :invoices
   has_many :account_payables
end
# not shown: Invoice and AccountPayable classes 
# with corresponding belongs_to associations

This always seems to work fine. Though as I say, when I read the help for polymorphic, I get the feeling I should be using it for the above case.

But then I think that can't be right. If my simple example above is polymorphic, then that means a model can only be "used" by one other model, unless you go polymorphic. This feels overly restrictive. Also, the polymorphic interface name examples tend to be verbs (e.g. "commentable", etc). How do I turn my above example into a verb? Companyable?

Perhaps the documentation is trying to tell me that its for when a model instance can only belong to one of the associated entities and no more. For example if a company could only belong to Invoices or APs (not both). If this is the case, it seems to me it's more like Single Table Inheritance than plain old has_many/belongs_to. Using STI, I could have class Creditor < Company... and class Debtor < Company... and associate each with Invoices and AccountPayables respectively. In my above example, it's quite conceivable that a given Company is someone I invoice and also owe money to (i.e. AccountPayable)

Thanks for any clarification....


You state:

I find I have many cases where a model belongs_to more than one other model. For example if I have a Company model, an Invoice model and a AccountPayable model, I've found I can simply do:

But the example you give is actually of a model that "has_many" to more than one other model.

A model with a belongs_to means it must specifiy the foreign key, and if it belongs to more than one model, then how do you know which model the foreign_key refers to? It's this problem that polymorphic associations help you solve.

The example you give above is absolutely fine as it is, and does not require polymorphic associations.


You question is rather confusing and I think your models shouldn't be polymorphic.

An example of a scenario where polymorphism would be required.

(articles.rb) has_many article_comments
(photos.rb)  has_many photo_comments
(article_comments.rb) has_many article_comment_comments
(photo_comments.rb) has_many photo_comment_comments # repeat for comments on commments', comments etc.

v

(articles.rb) has_many comments, :as => commentable
(photos.rb) has_many comments, :as => commentable
(comments.rb) has_many comments, :as => commentable
0

精彩评论

暂无评论...
验证码 换一张
取 消