Topic: Deleting child object with Edge Rest interface...

I was just minding my own business, playing with Edge and the Restful interface it had, when mysql started giving me errors. I'm just playing around, but I'd like to know if I've hit a bug, incomplete feature, or if I misunderstood what I was supposed to write in.

In general, a world has many story_arcs, and a story_arc belongs to a world. Worlds are CRUDy and the Story Arcs have the CRU, but the D causes the following error:

Mysql wrote:

Mysql::Error: Cannot add or update a child row: a foreign key constraint fails: UPDATE story_arcs SET world_id = NULL WHERE (world_id = 1 AND id IN (1))

Here's some of the relevant code.
routes.rb

 
...
  map.resources :worlds do |world|
    world.resources :story_arcs
  end

story_arc_controller.rb
  def destroy
    story_arc = @world.story_arcs.find(params[:id].to_i)
    @world.story_arcs.delete(story_arc)

    respond_to do |format|
      format.html { redirect_to story_arcs_path }
      format.xml  { head :ok }
    end
  end


_story_arc.rhtml
<%= link_to 'Destroy', story_arc_path(:id => story_arc,
                                         :world_id => story_arc.world), :confirm => 'Are you sure?', :method => :delete %></td>

I'm all about messing with fire: I just want to know if I'm burning myself or if the stove is faulty.
Any help would be appreciative big_smile

Last edited by LifeIsGood (2006-11-11 15:26:40)

Re: Deleting child object with Edge Rest interface...

It sounds like you're using innodb tables and you've got a foreign key constraint set.  For the record this is an excellent way to set up your database.  The problem is that Rails is trying to set your world_id to null when there's no world with an id that really is null.

The solution would be to get Rails to bypass this UPDATE command and go straight to a DELETE command.

I haven't done any testing, but if you change

  story_arc = @world.story_arcs.find(params[:id].to_i)
  @world.story_arcs.delete(story_arc)

to:
  story_arc = @world.story_arcs.find(params[:id].to_i)
  story_arc.destroy

does that work?

Re: Deleting child object with Edge Rest interface...

Yes!
That worked wonderfully. Thank you for the explanation