Topic: Linking Tables

I have two models in my application. One is a client list and the other is a jobs list.  I've listed my model code below.  I want to create a relationship between the two models being one client can have many jobs.  I haven't created the controller yet as I thought I would sort out the models first. Where do I add a foreign key? Do I add it to the jobs model?

class CreateClients < ActiveRecord::Migration
  def self.up
    create_table :clients do |t|
      # t.column :name, :string
      t.column    :name,        :string
      t.column    :contact,     :string
      t.column    :phone,       :string
      t.column    :email,       :string
      t.column    :comments,    :text
    end
  end

  def self.down
    drop_table :clients
  end
end


class CreateJobs < ActiveRecord::Migration
  def self.up
    create_table :jobs do |t|
      # t.column :name, :string
      t.column  :date_visit,      :string
      t.column  :task,            :string
      t.column  :resource,        :string
      t.column  :date_start,      :string
      t.column  :date_end,        :string
      t.column  :time_allocated,  :string
      t.column  :time_spent,      :string
      t.column  :status,          :string
      t.column  :comments,        :text
    end
  end

  def self.down
    drop_table :jobs
  end
end

Re: Linking Tables

You would add a "client_id" column to the jobs table (of type integer). You would then set up the models like this:

class Client < ActiveRecord::Base
  has_many :jobs
end

class Job < ActiveRecord::Base
  belongs_to :client
end

Railscasts - Free Ruby on Rails Screencasts

Re: Linking Tables

Yes, you should!

Having recently learned all about associations the hard way, I am qualified to tell you that your  Jobs table needs a :client_id column, and your models need the following code.

In client.rb:

class Client < ActiveRecord::Base
  has_many :jobs
end

And in job.rb:
class Job < ActiveRecord::Base
  belongs_to :client
end

ActiveRecord will, awesomely, take care of everything else for you. You can call @job.client or @client.jobs like it ain't no thang.

God is a storyteller.

Re: Linking Tables

Thanks guys for the feedback. I've got more questions but I'll have a crack at it first and get back to you.

Thanks heaps.

Re: Linking Tables

Ok I'm ready to move on with this.  Basically now I've added a controller called home and a view called index.html

class HomeController < ApplicationController

  def index
    @clients = Client.client_list
    @jobs = Job.job_list
  end
end


and the view code below
<h1>COMPANY JOB LIST</h1>

<p>Hello Cruel World</p>

<% for client in @clients %>
<h2><%=h client.name  %></h2>
   
    <% for job in @jobs %>
      <p><%=h job.task %></p>
    <% end %>

<% end %>


Go to <%= link_to "Client List", :controller => 'clients' %><br />
Go to <%= link_to "Jobs List", :controller => 'jobs' %>


The problem I have is that I only want to list tasks related to that client under that client heading only.   So I'm guessing in need to add a condition on the for loop that checks the job.task to check that the client.id is the same as the job.client_id but I'm not sure what the syntax is to add the condition to the for loop.

Re: Linking Tables

No need to add a conditional check, just call "client.jobs".

<h2><%=h client.name  %></h2>
   
    <% for job in client.jobs %>
      <p><%=h job.task %></p>
    <% end %>

<% end %>

Railscasts - Free Ruby on Rails Screencasts