I am very new to Ruby and am starting to build an app for myself just to learn while I go.
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. 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:
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. Obviously, I would need each "collection" to have a long list of user_ids.
Is there a simpler way to do this? Is it possible to just make the collection.rb into the following?
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? 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.
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:
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
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?