Self referencing many-to-many

I’d like to create a recommended product feature on an app I’m working on and I’ve found a few articles about this topic but something is messed up and when I try to create a recommended product it only partially works.

In my DB I see that it successfully create a new row but only saves the product_id and the recommended_id is empty.

Here are some snippets of my code which I think is relevant to this topic. Let me know if you need more.

Any idea what might be the problem?

Try to add join_keys: [product_id: :id, recommended_id: :id], like this

many_to_many :recommended_products, Product, join_through: "recommended_products", join_keys: [product_id: :id, recommended_id: :id], on_replace: :delete
2 Likes

Thank you @kokolegorille! It works!