I have a form where I need to create two objects: sale and organization. This happens inside sales_controller.
For brevity's sake, the models and their attributes are:
sale
amount (integer)
organization_id (uuid)
organization
name (string)
sale belongs_to organization
organization has_many sales
That is what the sales new should look like. It has an input for sales, which user will enter, and it also has an input for Organization name, which user will input. Upon submission, user will have created a new sale (plus created a new organization, associated to that sale).
I am trying to figure out what is the best practice for create method inside sales_controller.
def create
@sale = Sale.new(sale_params) #assume sale_params permits organization_id and amount
@organization = Organization.create(params[:name])
#@sale.organization_id = @organization.id
if @sale.save
render json: @sale
else
render json: sale, status: :unprocessable_entity
end
end
@organization = Organization.create(...) does not feel right. Is there a way to do multiple object submission the "Rails"-way?
