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.id
  if a.b # => tests if a now has a valid B
    return true
  else  # if it could not create B
    [do something here]
  end
end

Perhaps if you give us a little more information on A and B, their
attributes etc., I can offer a little more advice?

G


On May 28, 4:05 pm, Vipin <[email protected]> wrote:
> On May 28, 6:42 pm, Gavin <[email protected]> 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)
> >     B.create! :a_id => a.id
> >   end
> > end
>
> but won't t also have the issue of validation.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to