[LRUG] Setting default roles in a user record
James Adam
james.adam at gmail.com
Tue Aug 7 08:14:39 PDT 2007
On 8/7/07, Tim Cowlishaw <tim at timcowlishaw.co.uk> wrote:
> Hmm... thinking about it, wouldn't after_create do the job?
I think there's an interesting point to be made here about avoiding
dependence on the Rails "magic" where it's not really necessary.
Certainly, if you *always* need a object to have certain defaults,
then you can start looking into overriding initialize or using the
callbacks. But in this case, do you really *always* want user objects
to be created in this way? Really?
If your model needs specific creation behaviour, putting that code in
a callback makes it less clear what your intentions are, particularly
when you are creating these instances in a controller.
With the magic:
class ThingController < ActionController::Base
def create
@thing = Thing.new(params[:thing]) # something *magical* goes on in here
if @thing.save
# etc...
But is that really better than:
class ThingController < ActionController::Base
def create
@thing = Thing.new_with_defaults(params[:thing])
if @thing.save
# etc...
IMHO, the second is not only clearer, but it's going to make it much
simpler to test, debug, and understand later down the line.
--
* J *
~
More information about the Chat
mailing list