[LRUG] Setting default roles in a user record

John Winters john at sinodun.org.uk
Tue Aug 7 03:25:14 PDT 2007


James Adam wrote:
> Oops - you probably want to have that user saved and returned from the
> method, but you get the idea :)
> 
> On 8/7/07, James Adam <james.adam at gmail.com> wrote:
>> Or, sidestep the whole "initialization" issue:
>>
>>   class User < ActiveRecord::Base
>>     def self.create_with_default_roles(*attributes)
>>       new_user = new(*attributes)
>>       Roles.find_all_default_roles.each do |role| # or whatever
>>         new_user.roles << role
>>       end
>>     end
>>     # etc...
>>   end
>>
>> My point is, you don't need this to happen *automagically*. Just
>> define a new method that does what you want, and call it wherever you
>> want this default user created.

This sounds a promising approach.  Could I just override the default 
"new" method with my own which sets default values if no parameters are 
passed in?

I don't think I want it saved before the method returns, because apart 
from anything else it would fail validation at that point.

Thanks for your help.

John



More information about the Chat mailing list