简体   繁体   English

我怎样才能使用户具有has_many个对象,但每个对象都可以属于许多用户?

[英]How can I make a user has_many objects but each object can belong_to MANY users?

I am very new to Ruby and am starting to build an app for myself just to learn while I go. 我对Ruby还是很陌生,并开始为自己构建一个应用程序,以便我在旅途中学习。
Sorry if this is a very simple problem but I can't seem to figure it out with all the searching I can do on associations. 抱歉,如果这是一个非常简单的问题,但是我似乎无法通过对关联的所有搜索来弄清楚。

Since I love collecting vinyl, I was hoping to build a site where a user can post their collections of vinyls. 由于我喜欢收集乙烯基,因此我希望建立一个用户可以在其中发布其乙烯基收藏的网站。 I started with a simple has_many/belongs_to association between my user model and vinyl model. 我从用户模型和乙烯模型之间的简单has_many / belongs_to关联开始。 I realized if two users have the same vinyl, it would be best for the second user to just find the previously uploaded one and add it to their collection (instead of having a bunch of duplicates in the database). 我意识到,如果两个用户使用相同的乙烯基,那么第二个用户最好只是找到先前上传的乙烯基并将其添加到他们的收藏中(而不是在数据库中有一堆重复的唱片)。

After some searching around, I decided on a has_many :through relationship where I made a third model called collections that has: 经过一番搜索后,我决定了一个has_many:through关系,在这里我制作了一个名为collections的第三个模型,该模型具有:

belongs_to :user
belongs_to :vinyl

So now my vinyl model has 所以现在我的黑胶模型

has_many :collections
has_many :users, through: :collections

and my user model has 我的用户模型有

has_many :collections
has_many :vinyls, through: :collections

However, when looking in the sqlite browser, it seems like each "collection" ends up with just one user_id and one vinyl_id. 但是,在sqlite浏览器中查看时,似乎每个“集合”最终都只有一个user_id和一个vinyl_id。 Obviously, I would need each "collection" to have a long list of user_ids. 显然,我需要每个“集合”都有一长串的user_id。

Is there a simpler way to do this? 有没有更简单的方法可以做到这一点? Is it possible to just make the collection.rb into the following? 是否可以将collection.rb设置为以下内容?

has_many :users
belongs_to :vinyl

Is it possible for A to has_many of B AND B to has_many back of A or is that just crazy? A可能有B的has_many,而B却有B的has_many多个,还是那么疯狂? Is this a stupid question? 这是一个愚蠢的问题吗?

Of course, I will have to do a lot of modifications in my controllers and views but I want to just make sure I have the database set up the right way first. 当然,我将不得不在控制器和视图中进行很多修改,但是我只想确保首先以正确的方式设置数据库。 Thank you all for any help! 谢谢大家的帮助!

In the first half of your question you describe exactly the solution you need. 在问题的前半部分,您将准确描述所需的解决方案。 You've done everything right. 您已正确完成所有操作。

Imagine you have two users A, and B, and three records, X, Y and Z. User A has record Y, user B has record Z ... but both have record X. 假设您有两个用户A和B,以及三个记录X,Y和Z。用户A有记录Y,用户B有记录Z ...但两个都有记录X。

You create your users: 您创建用户:

a = User.create # id: 1
b = User.create # id: 2

and your records: 和您的记录:

x = Record.create # id: 1
y = Record.create # id: 2
z = Record.create # id: 3

Then you set up your associations: 然后,您建立关联:

a.records << y
a.records << x
b.records << x
b.records << z

The product of this is 4 rows in your collections table: 这样做的产品在你的4行collections表:

id | user_id | record_id
------------------------
1  | 1       | 2           # user A, record Y
2  | 1       | 1           # user A, record X
3  | 2       | 1           # user B, record X
4  | 2       | 3           # user B, record Z

This is how the join table works. 这就是联接表的工作方式。 It connects each user to all of its records, and each record to all of its users. 它将每个用户连接到其所有记录,并将每个记录连接到其所有用户。

This allows you to do things like User.first.records or Record.first.users 这允许您执行诸如User.first.recordsRecord.first.users

You're on the right track ... keep going and it'll start to click. 您处在正确的轨道上……继续前进,它将开始单击。 Maybe rename your join table and model from collections to RecordOwnerships or something so that it makes more sense? 也许将您的RecordOwnerships表和模型从collections重命名为RecordOwnerships或更有意义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个新对象时,不能使用Emirates_to / has_many关系设置user_id字段 - Can't set user_id field when creating a new object, using a belong_to / has_many relationship 数据库关联,类has_many和belonge_to可以属于同一对象吗? - Database Association, Can a class has_many and belong_to the same object? Rails has_many / belong_to协会 - Rails has_many/belong_to Associations 在rails中的has_many中的关联 - Associations in has_many , belong_to in rails 如何在ruby中为用户和任务模型之间的has_many,belong_to关联创建迁移脚本 - How to create migration script for has_many, belong_to association between user and task model in ruby has_many和belong_to关联出现问题,找不到方法 - Issue with has_many and belong_to association, can't find method 在Ruby on Rails中,模型“ has_many”和“ belong_to”如何使用除主ID之外的其他字段? - In Ruby on Rails, how can a model “has_many” and “belong_to” using a different field other than primary ID? 关于belong_to,has_one,has_many的困惑 - Confusion regarding belong_to, has_one, has_many 用户has_many:课程,但课程不属于:用户 - user has_many :courses, but course doesn't belong_to :user 更新has_many / belong_to关系中大量关联对象的行(Rails 4,PostgreSQL 9.4,activeadmin) - Update massive number of rows of associated objects in has_many/belong_to relations (Rails 4, postgresql 9.4, activeadmin)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM