[Rails] Re: save new model with child model

Peter Michaux petermichaux at gmail.com
Mon May 1 04:26:01 GMT 2006


Hi,

Please tell me there is something simpler.

Peter

On 4/26/06, Peter Michaux <petermichaux at gmail.com> wrote:
> On 4/25/06, Peter Michaux <petermichaux at gmail.com> wrote:
> > Hi,
> >
> > I am having a problem saving a new model that has an associated child
> > model. If a new ticket is being created and will have one comment to
> > go with it then I tried this in my action
>
> [snip]
>
> I am current going to all this effort which just seems wrong.
>
>       @ticket = Ticket.new(params[:ticket])
>       @comment = Comment.new(params[:comment])
>
>       if @ticket.save
>         @comment.ticket_id = @ticket.id
>         @ticket.comments << @comment
>         if @comment.save
>           @notice = "Ticket added"
>         else
>           @ticket.destroy
>           @notice = "Sorry, unable to add ticket."
>         end
>       else
>         @notice = "Sorry, unable to add ticket."
>       end
>
> There must be a simpler way. Any ideas?
>
> Thanks,
> Peter
>


More information about the Rails mailing list