I have a Store model, which has an id and other fields. I have a Product model which has an id and other field To join them I use Amount model. Here are the models:
class Product < ApplicationRecord
has_many :amounts
has_many :stores, through: :amounts
end
class Store < ApplicationRecord
has_many :amounts
has_many :products , through: :amounts
end
class Amount < ApplicationRecord
belongs_to :store
belongs_to :product
end
Amount has in_stock property. I want my json to look like this:
{stores: [
{"id": 1,
"name": "New Store",
"products": ["id": 1, "name": "Toothbrush", "in_stock": 4000]}
]}
I tried render json: {stores: stores}.to_json(include: :products), I also tried a nested include but it didn't work as well, it showed output for all amounts