Say I have these two classes: class Client < ActiveRecord::Base has_one :setting, :dependent => :destroy end class Setting < ActiveRecord::Base belongs_to :client end Is there any way to write an init method that will create and assign the client a setting before evaluating params, but otherwise works ''as normal''? Thanks, Isak --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Does this work for you: class Client < ActiveRecord::Base has_one :setting, :dependent => :destroy after_create{ setting = Setting.create() } end This will create an associated setting when the Client is first saved (inserted) into the database. Cheers, Max On 8/31/06, Isak Hansen <isak.hansen-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Say I have these two classes: > > class Client < ActiveRecord::Base > has_one :setting, :dependent => :destroy > end > > class Setting < ActiveRecord::Base > belongs_to :client > end > > Is there any way to write an init method that will create and assign > the client a setting before evaluating params, but otherwise works ''as > normal''? > > > Thanks, > Isak > > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Max Muermann wrote:> Does this work for you: > > class Client < ActiveRecord::Base > has_one :setting, :dependent => :destroy > after_create{ setting = Setting.create() } > end > > This will create an associated setting when the Client is first saved > (inserted) into the database. > > Cheers, > MaxTry this instead class Client < ActiveRecord::Base has_one :setting, :dependent => :destroy after_create :create_setting private def create_setting self.setting = Setting.create end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---