Overview

Metadata is a way to add additional information to Spree objects. There are two types of metadata:

Public Metadata

Public metadata is available via Storefront API without any authentication. It is stored in the public_metadata JSONB column of the table. This kind of metadata should be only used for public information that is visible to all users, eg. additional information about the object.

Private Metadata

Private metadata is only available via Platform API and requires authentication. It is stored in the private_metadata JSONB column of the table. This kind of metadata should be only used for private information that is not visible to the public, eg. IDs in external systems, private notes, etc.

Usage

Adding metadata to an object

taxon = Spree::Taxon.find(1)
taxon.public_metadata = { "google_category_id": "123" }
taxon.save!

Fetching metadata from an object

taxon = Spree::Taxon.find(1)
puts taxon.public_metadata["google_category_id"]

Querying objects by metadata

Spree::Taxon.where("public_metadata->>'google_category_id' = ?", "123")

For Products it’s recommended to use Properties or Option Types instead of Metadata.