How to use node reference fields, relationships and arguments together with views

When working with subject matter as complicated as Drupal there is always something new to learn. I remember seeing the relationship section in views 2 for a long time before I actually ended up needing it, and when I did need it, I had to struggle with it for a while before getting it to work. Not that it’s really difficult, but you have to do it right. I’m putting this down in writing for you, the up late at night, pulling ones hair out because you have to have this working by tomorrow developer, and for myself, just in case I forget it (Ah, the irony to find your own tutorials when googling a question).

Ok here’s the short and simple version of the way it works:

Say you have a content type “product” and you have a content type “car brand”. Now these products have a node reference field for the type of car they are made for (these products are accessories like spoilers or seats for a car). How do you go about making a view that accepts an argument and returns the products made for the car type in question?
By the way if you are wondering why in this case we didn’t just use taxonomy, it’s because for the site in question (a real site) the car type actually needed a lot more information attached to it than a taxonomy term would allow (such as images, a logo, specs etc…).

To get the described scenario to work you basically have to do the following. Go to the relationship section in views and choose the group labeled content. Then from the list of fields choose the node reference field that you used to link the two content types together.

Now here’s the tricky part, and the part where I banged my head against a wall for an hour or so. When it comes time to create your argument DON’T choose the content group and your node reference field under it. It won’t work. Instead choose the node group, then choose either nid or title depending on how you want to send your arguments (I tend to use titles these days for SEO purposes). Then in the relationship drop down which is now available in the argument section you choose the relationship that you created earlier. Voila, your view should now work.

You can set the validation as you see fit for your application, that’s another topic all together.

Anyway, I hope that helped you.