Skip to content

Latest commit

 

History

History
28 lines (19 loc) · 845 Bytes

nested_models.md

File metadata and controls

28 lines (19 loc) · 845 Bytes

Nested Models

In some cases, you might need to have a stored model as an attribute of another one:

class Supplier
  include StoreModel::Model

  attribute :title, :string
end

class Configuration
  include StoreModel::Model

  attribute :supplier, Supplier.to_type
end

To make it work with nested Rails forms (i.e., define a method called #{attribute_name}_attributes=) you should add accepts_nested_attributes_for :supplier, which works in the same way as the built-in Rails method, to the parent model.

Validation

If you want to validate the nested model, you must use the StoreModelValidator validator:

attribute :supplier, Supplier.to_type # existing attribute definition
validates :supplier, store_model: true