How to delete data in in project table and task table with just a relational id?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Exequiel
    Contributor
    • Jul 2012
    • 288

    How to delete data in in project table and task table with just a relational id?

    Hi I have 2 tables in database, and i want to know how i can delete the data in tasks table after i delete the data in projects table, the task table is connected by a project_id in projects table's id,

    heres the tables. . .

    Code:
    [B] Table for projects"[/B]
       Column   |            Type             |                       Modifiers                       
    ------------+-----------------------------+-------------------------------------------------------
    [B]id[/B]         | integer                     | not null default nextval('projects_id_seq'::regclass)
    title      | character varying(255)      | 
    description | character varying(255)      | 
    user_id    | integer                     | 
    created_at | timestamp without time zone | not null
    updated_at | timestamp without time zone | not null
    addbyemail | character varying(255)      |  
    
                                          [B] Table for tasks"[/B]
         Column      |            Type             |                     Modifiers                      
    ------------------+-----------------------------+----------------------------------------------------
    id               | integer                     | not null default nextval('tasks_id_seq'::regclass)
    todo             | character varying(255)      | 
    [B]project_id[/B]       | integer                     | 
    assigned_user_id | integer                     | 
    created_at       | timestamp without time zone | not null
    updated_at       | timestamp without time zone | not null
    i want to delete the data using rails.

    heres my code in db/migrate for projects table
    Code:
    class CreateProjects < ActiveRecord::Migration
      def change
        create_table :projects do |t|
          t.string :title
          t.string :decription
          t.integer :user_id
    
          t.timestamps
        end
      end
    end
    migration for tasks table
    Code:
    class CreateTasks < ActiveRecord::Migration
      def change
        create_table :tasks do |t|
          t.string :todo
          t.integer :project_id
          t.integer :assigned_user_id
    
          t.timestamps
        end
      end
    end
    heres my code in my app/controllers/projects_contro ller.erb
    Code:
    def destroy
        @project = Project.find(params[:id])
        @project.destroy
    
        respond_to do |format|
          format.html { render action: 'show' }
          format.json { render :json => { :status => 'Ok', :message => 'Project was successfully Deleted'} }
        end
      end
    Please help me. any body please? tank you so much...
Working...