Given a data model with a many-to-many relationship, naturally we’d want to include a joiner model. Just a heads up, we’re using a piece of my module 5 project, so it might not completely make sense big picture wise.
Anyway, we have the following relationship.
With attributes, we see that the joiner table, in this case called the “Personal Library” has the foreign keys of the User and Workout models.
The standard JSON rendering in the personal_libraries controller would be the following:
def indexpersonal_libraries = PersonalLibrary.allrender json: personal_libraries, except: [:created_at, :updated_at]end
Note: “except: [:created_at, :updated_at]” is a personal preference.
With the above rendering, our JSON file would look something like this when using a GET fetch request:
Note: There’s more to it but I didn’t feel like pasting the entire JSON file.
The issue with this is that we only have access to the id’s of our other two instances. We would have to do a lot of front end work to render the user and workout objects.
If our backend is set up accordingly, we can easily do the following commands in our console:
#=> (workout object)PersonalLibrary.first.workout.name
#=> (workout name)PersonalLibrary.first.user
#=> (user object)PersonalLibrary.first.user.name
#=> (user name)
Here’s a sample of what that may look like in the console:
Cheat the JSON Serialization System
Serialization is great and all, but here’s how to cheat the system. Inside your controller, simply add “include: [:workout, :user]” so the full code of your index method would be:
def indexpersonal_libraries = PersonalLibrary.allrender json: personal_libraries, except: [:created_at, :updated_at], include: [:workout, :user]end
Now our new JSON file looks like:
Now in our front end, we can use the same commands as in our back end console!…Well, you know…through a proper GET fetch request and variable assignment and stuff.
Things to keep in mind:
- This will only work if the relationships in the back-end are set up correctly. The way to check that is through your console as shown above in the VS Code snippet.
- Creating a new instance of PersonalLibrary stays the same.
PersonalLibrary.create(user_id: 7, workout_id: 8)
BUT, be sure to also render your json for your create method accordingly!
def createpersonal_library = PersonalLibrary.create(personal_library_params)render json: personal_library, except: [:created_at, :updated_at], include: [:user, :workout ], status: 201end
3. Random but relavant to data models with underscores in between words, the URL to the PersonalLibrary is through:
4. This process can be done at any point. If you find that you’re not getting the info you need from your joiner models or from models that have many of something else, implementing this strategy is a good first step to solving that issue.
5. This is cheating the JSON serializer because we didn’t create a serializer file. We only made adjustments to our controller of the join table.
6. Please keep in mind that this isn’t really DRY code… it’s just a quick fix for coding bootcamp projects 😬 (It’s best practice to eventually learn serialization properly. But until then and you’re at a time crunch, we’re “cheating”)
Happy coding ❤