Best Practices for Multiple Model Creation

So I have a few models that need to be created, but only based on a few
inputs. There is no need for nested attributes, in fact I would rather
avoid this.

The way I have been doing it is like so

class WidgetsController < ApplicationController
def create
respond_to do |format|
if current_user.create_widget(params[:foo])
flash[:success] = “Processed your request”
else
flash[:error] = “There was a problem processing your request”
end
format.html { redirect_to widgets_path }
end
end
end

class User < ActiveRecord::Base

def create_widget params
User.transaction do
widget = self.widgets.build.save!
widget.blinky_lights.build(params[:foo]).save!
true
end
end

end

In this contrived example, Widget is basically an entity that other crap
will hang from. Is this even a decent build pattern or does someone have
a
better idea of how to handle a case like this.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs