Working with model associations can be difficult. While building my mock , Ruby on Rails application. and relationships made sense, but the addition to many-to-many— —relationships are where it got weird. I ran into several issues while building my application and this is how I solved them. Central Perk has_many belongs_to has_many through Model Behavior For my project, I planned on having three models: Users (baristas), Orders and Menu Items (products). A user could have many orders. An order, which belongs to a user, could have many Menu Items With those models, programmatically, I wanted to be able to do the following: Create a new Order and automatically associate it with a User. shannon.orders.create Be able to see all orders that belong to a User. shannon.orders See all MenuItems (products) in a specific order. order.menu_items After setting up my models, everything seemed to be working up until . Currently, an Order with an attribute of could only have one value. Meaning, MenuItem per Order. Can you imagine if you had to get back in line and start a new order for each item you wanted to buy at a coffee shop? order.menu_items menu_items one Ross and Rachel looking confused This is where a relationship came in. has many through Many to Many If I wanted more than one MenuItem, per Order, the result of needed to be an array. This is where a four, model comes in. order.menu_items OrderItem acts as a join table, with foreign keys to the and models. In this example, think of each OrderItem record has a transaction instance, representing one and one at a time. OrderItem Order MenuItems Order MenuItem An Order would essentially be a collection of all records with the same . I was a step closer to figuring out what I needed. OrderItem order_id But? At first, an model made sense. OrderItem Until, it didn’t. Would I need to call to see all the items in that order? My app had a model too. How do you build a has_many through a relationship when there are more than three models? order.order_items.menu_items User Pheobe running around screaming "What do I do?" In reality, has_many through only works with three models. But, through other associations, it the functionality of those models. If I wanted to know how many MenuItems were in the first order, created by a specific user I could call something like this: . extends user.orders.first.menu_items.count Visually, I thought of the relationships between the four models as looking like this: Visualization of the has_many through relationship of User, Order (which contains OrderItems) and Menu Items This was making sense! I would not need to reference directly. ActiveRecord does that work for me. Since an has many , referencing the would gives me direct access to . OrderItems Order OrderItems Order MenuItems My finalized models now looked like this: has_secure_password has_many belongs_to has_many has_many , belongs_to belongs_to has_many has_many , < ApplicationRecord class User :orders end < ApplicationRecord class Order :user :order_items :menu_items through: :order_items end < ApplicationRecord class OrderItem :order :menu_item end < ApplicationRecord class MenuItem :order_items :orders through: :order_items end Params With the associations complete, I needed a form to create the object. At first, everything seemed to be working. But after looking closer at the console, I realized the transaction was getting rolled back and orders were not being saved to the database. Order I noticed the key was listed in my strong params, but I was getting a " is not permitted error". :menu_items_id :menu_items_ids To try and resolve this, I worked in the console, testing things out, bit by bit until I could pinpoint where I was getting stuck. In the console, I could successfully do the following. Create an order. order = Order.create(user_id: 1, name_for_pickup: "Rachel", menu_item_ids: [1,2,3]) . View the value of menu_items on an order order.menu_items // [1,2,3] . Add an item to an order order.menu_items << item . Save the order order.save Then it hit me. Rails was right in not permitting the param. I thought I needed to create an order. Instead, I needed to create an order, . menu_item_ids find the menu items by id (menu_items_id, which was the unpermitted param) and shovel them into the array I updated my create order method from this: @order = Order.create(order_params) @order.save redirect_to order_path(@order) render def create if else :new end end To this @order = Order.create(order_params) items_to_add = params[ ][ ] items_to_add.each item_id != item_id = item_id.to_i item_to_add = MenuItem.find_by_id(item_id) @order.menu_items << item_to_add @order.save redirect_to order_path(@order) render def create :order :menu_item_ids do |item_id| if "" end end if else :new end end And it worked! Chandler jumping on a coffee table and doing a happy dance Lessons Learned In summary, if you are running into issues with object relationships, try the following: Verify that the params are correct Typos can instantly cause object creation to fail. Pluralization like vs are also something to look out for. , which may cause downstream effects if you are expecting an integer or boolean. menu_item_id menu_item_ids All params are strings All model attributes are listed in strong params Strong params help to prevent users from injecting harmful data into your database via the form. If strong params lists only , and can be submitted in a model, the transaction will fail (and not write to your database, yay!) if an attribute of was within your params. :name :email :password User :not_a_hacker Use .create! when testing will give more information into what validations may be causing errors. For example, in my app, an must have as (barista) user_id associated with it. Running in the console would not tell me much, but running would print out an error like . create! Order User Order.create() Order.create!() A User must exist Append `.valid?` to objects An object may be updating, but, is it saving properly to the database? For example, if (an empty object, which will not validate because there is no ), adding will return false. order = Order.create() :user_id .valid? Summary Model associations can be difficult and frustrating, but not impossible to work with. With some careful debugging, you can be a master of model associations in no time. Resources y What Are Rails Parameters & How to Use Them Correctl Active Record Associations Photo by Annie Spratt on Unsplash Previously published at https://shannoncrabill.com/blog/ruby-on-rails-debugging-model-associations/