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.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/QNN5Pozi55QJ.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedSep 24, '12 at 5:30a
activeSep 24, '12 at 5:30a
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Matthew Johnston: 1 post

People

Translate

site design / logo © 2022 Grokbase