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
@ticket = Ticket.new(params[:ticket])
@comment = Comment.new(params[:comment])
@ticket.comments << @comment
if @ticket.save
@notice = "Ticket added. Thanks for contributing!"
@ticket = Ticket.new
@comment = Comment.new
else
@notice = "Sorry, unable to add ticket."
end
The save always fails because the comment does not have a ticket_id
specified. How can I specify the ticket_id for the comment if the
ticket id doesn’t even exist before I call save. I thought Rails would
take care of this for me when I use the << or push(). (Rails 1.0)
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