I'm definitely down to use some other sort of association if you think there is a better way.
So my goal is to have a travel app where I have a User who can have many Trips and many Destinations through Trips. I also want them to be able to create their own Destinations. The idea of this is so that other Users could add these Destinations to their Trips and leave comments on them and only the User who created the Destination can edit that Destination. So maybe I could just do the has_many through association, but when a User creates a Destination, it also has a hidden field with something like a created_by_id that stores that User's id to be able to reference back to it? Open to any other ideas as well.
for each given trip, which destinations she added?
The problem with this one is that a destination belongs to a single trip, so if two people want to add "Bora Bora" to their trip, they can't, unless you duplicate the record.
To allow two trips to have Bora Bora you need to have a many-to-many relationship between them (a trip has multiple destinations, a destination can appear in multiple trips).
So our models would become:
classUserhas_many:tripsendclassTripbelongs_to:userhas_many:trip_destinationshas_many:destinations,through: :trip_destinationsendclassTripDestination# come up with a better name :Dbelongs_to:tripbelongs_to:destinationendclassDestinationhas_many:trip_destinationshas_many:trips,through: :trip_destinationsend
This way, you can know the following:
which trips Alice has created
which destinations belong to each trip
which trips include each destination
In your database you will have a list of possible destinations and each user can add them to their trips.
Next, we need to let these users create their own destinations, instead of just using the pre-populated ones.
The only difference between a predefined destination and a user created is the notion of creator. Other than having a flag predefined or custom or something like that you need to attach the id of the creator to a destination.
Something like:
classUserhas_many:tripshas_many:custom_destinations,class_name: "Destination",foreign_key: "creator_id"endclassTripbelongs_to:userhas_many:trip_destinationshas_many:destinations,through: :trip_destinationsendclassTripDestination# come up with a better name :Dbelongs_to:tripbelongs_to:destinationendclassDestinationbelongs_to:creator,class_name: "User",foreign_key: "creator_id"has_many:trip_destinationshas_many:trips,through: :trip_destinationsend
The reason why I'm calling the foreign key creator_id and not user_id is for future proofing. Now you're mapping a relationship of creator/creation between a user and a destination, you might want to add another type of relationship between the two entities in the future, and add another key like moderator_id or whatever. They all point to the User table, they just point to separate concepts mapped to different foreign keys.
I haven't tested it but it should work. You need to create the various foreign keys in the migration:
A tip: add the creation timestamp to the join table, so you'll know for free when the user has added the destination to their trip.
I hope I didn't forget something, let me know in case :-)
ps. my favorite gem to model permission (you're going to need something like it to describe the various actions a user can do, on destinations and comments) is Pundit
Awesome! I think that is exactly what I’m looking for! I’ll try it out and let you know how it goes. And thanks for the tip on Pundit! Excited to see how that works :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I'm definitely down to use some other sort of association if you think there is a better way.
So my goal is to have a travel app where I have a User who can have many Trips and many Destinations through Trips. I also want them to be able to create their own Destinations. The idea of this is so that other Users could add these Destinations to their Trips and leave comments on them and only the User who created the Destination can edit that Destination. So maybe I could just do the has_many through association, but when a User creates a Destination, it also has a hidden field with something like a created_by_id that stores that User's id to be able to reference back to it? Open to any other ideas as well.
Bear with me :-)
If I'm not mistaken:
A first version could be:
This way you can ask, given user Alice
The problem with this one is that a destination belongs to a single trip, so if two people want to add "Bora Bora" to their trip, they can't, unless you duplicate the record.
To allow two trips to have Bora Bora you need to have a many-to-many relationship between them (a trip has multiple destinations, a destination can appear in multiple trips).
So our models would become:
This way, you can know the following:
In your database you will have a list of possible destinations and each user can add them to their trips.
Next, we need to let these users create their own destinations, instead of just using the pre-populated ones.
The only difference between a predefined destination and a user created is the notion of creator. Other than having a flag
predefinedorcustomor something like that you need to attach the id of the creator to a destination.Something like:
The reason why I'm calling the foreign key
creator_idand notuser_idis for future proofing. Now you're mapping a relationship of creator/creation between a user and a destination, you might want to add another type of relationship between the two entities in the future, and add another key likemoderator_idor whatever. They all point to theUsertable, they just point to separate concepts mapped to different foreign keys.I haven't tested it but it should work. You need to create the various foreign keys in the migration:
Destination.creator_idA tip: add the creation timestamp to the join table, so you'll know for free when the user has added the destination to their trip.
I hope I didn't forget something, let me know in case :-)
ps. my favorite gem to model permission (you're going to need something like it to describe the various actions a user can do, on destinations and comments) is Pundit
Awesome! I think that is exactly what I’m looking for! I’ll try it out and let you know how it goes. And thanks for the tip on Pundit! Excited to see how that works :)