Last active
August 6, 2018 05:28
-
-
Save queq1890/f6ab3f9cc792fbf52729da435a4d2a1e to your computer and use it in GitHub Desktop.
Building children models without using accepts_nested_attributes_for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductForm | |
include ActiveModel::Model | |
include ActiveModel::Attributes | |
attr_accessor :images | |
attribute :deliverable_days, :integer | |
attribute :detail, :string | |
attribute :name, :string | |
attribute :price, :integer | |
attribute :shipping_fee_charges_on, :integer | |
attribute :shipping_from, :integer | |
attribute :status, :integer | |
def images_attributes=(attributes) | |
@images ||= [] | |
attributes.each do |i, image_params| | |
@images.push(ProductImage.new(image_params)) | |
end | |
end | |
def save | |
# implement as you like | |
# @product_form = ProductForm.create(attributes) | |
# @product_form.children.create | |
end | |
end | |
# In some controllers | |
# def new | |
# @product_form = ProductForm.new(images: [Image.new]) | |
# end | |
# def create | |
# @product_form = ProductForm.new(product_form_params) | |
# @product_form.save | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment