[英]Rails: How can I implement this relationship between these models? habtm? habtm through? Polymorphic?
I am storing my files on Amazon s3. 我将文件存储在Amazon s3上。 Each file has many "consumers" and these consumers can be of any type(users, external apps, business_listings, etc). 每个文件都有许多“消费者” ,这些消费者可以是任何类型(用户,外部应用程序,business_listings等)。 This calls for a many to many relationship. 这需要多对多关系。 However, I also want to store certain attributes about each relationship. 但是,我也想存储有关每个关系的某些属性。 So I think that the best way to approach this would be to create a *has_and_belngs_to_many_through* relationship between the "s3_files" and the consumers 因此,我认为解决此问题的最佳方法是在“ s3_files”和使用者之间创建* has_and_belngs_to_many_through *关系
The question now is, since there are many type of consumers, I need to use polymorphic associations. 现在的问题是,由于有许多类型的使用者,因此我需要使用多态关联。
So based on this I would take the following steps: 因此,基于此,我将采取以下步骤:
1) Create a model: "resource_relationship"
2) Have these fields in it:
t.string: consumer_type
t.string: s3_fileType
t.string: file_path
t.string: consumer_type
t.integer: s3_file.id
t.integer: consumer.id
t.integer: resource_relationship.id
3) Add to the model:
belongs_to: s3_file
belongs_to: consumer
4) add_index:[s3_file.id, consumer.id]
5) Inside: s3_file
nas_many: resource_relationships
has_many:consumers :through=> :resource_relationships
6) Inside: Users
nas_many: resource_relationships
has_many:s3_files :through=> :resource_relationships
7) Inside: business_listings
nas_many: resource_relationships
has_many:s3_files :through=> :resource_relationships
My confusion lies in the polymorphic part of this relationship. 我的困惑在于这种关系的多态部分。
Since I donot have a "Consumer" model. 由于我没有“消费者”模型。 I donot understand how that fits into this equation. 我不明白这怎么适合这个方程式。 I saw some tutorials online and came up with the above method and concept, but, Im uncertain if this si the correct way to approach this problem. 我在网上看到了一些教程,并提出了上述方法和概念,但是,我不确定这是否是解决此问题的正确方法。
Essentially, I understand here that "Consumers" is like an interface that all the models "implement" and then the S3_file model can associate itself with any model that has "implemented" this interface. 从本质上讲,我在这里理解“消费者”就像一个接口,所有模型都“实现”,然后S3_file模型可以将自身与“实现”该接口的任何模型关联。 Question is how and where do the models: users and business_listings "implement" this "Consumer" interface. 问题是如何以及在哪里进行这些模型:用户和business_listings“实现”此“消费者”界面。 How do I set that up? 我该如何设置?
Whenever you want to store attributes about the relationship between to models in a many-to-many relationship, generally the relationship is :has_many, :through . 每当您要以多对多关系存储有关模型之间关系的属性时,通常该关系为:has_many,:through 。
I'd recommend beginning with this simpler approach and then considering polymorphism later, if you need to. 我建议从这种更简单的方法开始,然后再根据需要考虑多态性。 For now, begin with just having a consumer_type
attribute you can use if you need it. 现在,从仅拥有一个consumer_type
属性开始,如果需要的话可以使用。
consumer.rb: Consumer.rb:
class Consumer < ActiveRecord::Base
has_many :resource_relationships
has_many :s3_files, through: :resource_relationships
validates_presence_of :consumer_type
end
resource_relationship.rb: resource_relationship.rb:
class ResourceRelationship < ActiveRecord::Base
belongs_to :consumer
belongs_to :s3_file
end
s3_file.rb s3_file.rb
class S3File < ActiveRecord::Base
has_many :resource_relationships
has_many :consumers, through: :resource_relationships
validates_presence_of :s3_fileType
validates_presence_of :file_path
end
Notice that this simplifies as well the need to have attributes in the resource_relationships
table -- those attributes are really attributes of the consumer
and s3_file
models anyway, so it may be best to just keep them there. 请注意,这也简化了在resource_relationships
表中具有属性的需求-那些属性实际上确实是consumer
模型和s3_file
模型的属性,因此最好将它们保留在那里。
:has_many, :through
I generally find to be more flexible and easier to modify as the relationships between your models changes. :has_many, :through
随着模型之间关系的改变,我发现通常更灵活,更容易修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.