There are some concepts that are just downright tough to wrap your head around the first go-round. This is not always because the specific item being learned is very hard in itself but because of everything related to the specific item is complex as well. Rails Nested Forms with the “Has Many Through” relationship has both. In spades. To top it off, it’s a shining light of a concept, I mean, when you think of what is going on behind the scenes, it’s pretty amazing. Similar to generators, you have access to so much power, but you are making a deal with the devil, and that always comes with a price. (Fwiw, I am no mean villainizing Rails, I think it’s fantastic).
The first aspect that is challenging is, you will quickly go from hand crafting most of your code to having Rails and Active Record doing a lot of the chores. But once you hit working with Nested Forms, there is a shift to rooting into the Object Model which ties in a deeper functionality that you haven’t worked with thus far if you are following the Learn curriculum. Hopefully you didn’t snooze through those Ruby courses, because you will need to have some of that stuff second-nature or you will never grasp it. You just won’t because it is a dependency no matter which way you slice it. Now, while you could fake some stuff like just using “accepts_nested_attributes_for” in each model, you are going to be at a disadvantage because there is inherently a lot of flexibility with building out your own methods. Plus, to be honest, that will help you mentally bridge the gap. In fact, I’d challenge you to do both and see the advantage of the automated method vs. creating your own. This will help solidify the concept.
The second aspect which is challenging is that now everything gets connected and you have so many options to solve these problems. The big issue here is, when dealing with Capybara specifics, the design of the test becomes a more specific driver for your solution. Avi Flombaum guides you through some of this in a video review, and I encourage you to watch it once you get through most of the lab. It will be more valuable to see the information once you have worked through building some of your own pieces. There are just too many ways to work with all of this connected data, but I would encourage you to work with chained methods for the controller/routes that you are working with. Sometimes this will get blurry due to what the test is asking. For instance, should you try to work with a comment object direclty in the post_show route, or should you try to work with @post.comments on your form? You may or may not be able to form a clean solution depeding on what direction you go. I will say, when you see really specific content in the test like: “ fill_in(‘comment_user_attributes_username’ “ that is really leaning on you to think about what way you are going to structure your solution.
This kind of brings me to the third challenge with all of this. If you’re a fan of Rick and Morty, do you remember how Summer’s employer (the Devil) was selling a typewriter that generates best selling murder mysteries, and then makes them happen in real life? Well that’s kind of how I feel about some of the Rails magic. On one hand, it is super fantastic and so so powerful, almost too powerful. The price is, that “ya’ll got some learnin’ to do”! Along with that is the inherent danger to just blindly use those methods. A lot of this is learn-by-doing because you really need the repetition on a theme to see how those form_for builders make stuff happen, and how these associations all come together between models. Fortunately Flatiron has once again, done a good job of prepping students for this portion of the course. I will say though, that even with all of that, it’s really hard to see how all of the parts interrelate, and it’s going to be confusing, especially at first. No two ways about it you are going to spend quite a bit of time making a lot of code that doesn’t even work. What I found most frustrating was knowing how I handled the forms in Sinatra, but then getting totally boggled by Rails. I think that because the application is so vast and the methods so many, that finding examples of how your brain wants to envision the problem is tough! Interestingly, it seems many of the examples, although helpful, seem to be lacking any form of explanation…kind or repeating the “just do this” theme (I feel this may reinforce the problem at hand!).
To sum it up, learning these concepts together is quite a challenge. However, there are a few tips I would like to share that has made this part of the course bearable and honestly without them I am not sure how I would have done it.
First thing’s first.
- You need to be in the console. Especially on the “Has Many Through in Forms” lab because a lot of code is given to you for starters. Drop in and familiarize yourself with the objects, but more importantly the relationships. You will quickly see the flexibility available in how you can put together your forms.
- Make a seed file. In your seed file, make sure you add one (or several) of each object and save them off. This can be done either before or after working with the console, I suggest no real order and working often with both! You will then clearly be able to see what you can and cannot do with your models. This will also be super critical when you are working in the rails server. If I hadn’t created my own seeds, there is no way I would have gotten through the lab, or if I did, I would have had a lot of unnecessary pain. Also, a little bit of time spent here will give great benefits in more quickly reaching your goals.
- Get a database browser. I have spoken about this before, when working with SQLite it is really a good idea, but when you start looking at these complex models and object relationships, it is really helpful to have a quick visual. That, and you can get rid of junk that you will create. Your database will cause your code to fail sometimes, usually because of a stale relationship or an empty association, but with the browser you can easily see that.
All in all, here is how I feel at the end of this portion: