[Rails] Re: how to create dependent record automatically

2009-05-28 Thread Vipin
> Perhaps if you give us a little more information on A and B, their > attributes etc., I can offer a little more advice? > Well, what I am trying to achieve here is that no way two records are created in B such that they belong to same record in A. For clear explanation let me try to map the pr

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread Gavin
yes and no after_create is called after A has been saved, so it will be valid. You'll have to make sure that the params for B are valid though By calling create! instead of create, rails will raise an exception if B is not valid but you could also write; after_create(a) B.create :a_id => a.

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread Vipin
On May 28, 6:42 pm, Gavin wrote: > Vipin, > > wouldn't it be better moving this out of the controller altogether? > > If every A should have a B then I'd create an observer - "AObserver" > > and add an after_create callback: > > class AObserver < ActiveRecord::Observer > >   def after_create(a)

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread JL Smith
I would go with the observer so you can keep the logic in the model (observer)...skinny controllers, fat models and all. http://guides.rails.info/activerecord_validations_callbacks.html#observers --~--~-~--~~~---~--~~ You received this message because you are subsc

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread Vipin
On May 28, 6:02 pm, tomrossi7 wrote: > I wonder if @b.save is returning false?  Maybe a validation issue? > Have you tried throwing a debugger statement in there? > yea it turned out to be exactly the 'save' failure due to validation. thanks --~--~-~--~~~---~--~-

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread tomrossi7
Yeah, it really belongs in the model... On May 28, 9:42 am, Gavin wrote: > Vipin, > > wouldn't it be better moving this out of the controller altogether? > > If every A should have a B then I'd create an observer - "AObserver" > > and add an after_create callback: > > class AObserver < ActiveRec

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread Gavin
Vipin, wouldn't it be better moving this out of the controller altogether? If every A should have a B then I'd create an observer - "AObserver" and add an after_create callback: class AObserver < ActiveRecord::Observer def after_create(a) B.create! :a_id => a.id end end --~--~

[Rails] Re: how to create dependent record automatically

2009-05-28 Thread tomrossi7
I wonder if @b.save is returning false? Maybe a validation issue? Have you tried throwing a debugger statement in there? On May 28, 7:09 am, Vipin wrote: > hi > I am facing a problem. I have two models A and B which have relations > as follows > > A has_one B > B belongs_to A > > Now I want to