Basic Rails Database Foreign Key question

P

portal33

Hi, I'm still waiting on my Agile Rails book to arrive - in the
meantime ...
As an exercise I'm making a Help Desk application.
If I have a Problems table with two foreign keys to a People table, how
do I set up my models, problems_controller list method, and then simply
display, say my problem submitter person? Here is what I have so far:

Problems table with columns:
id
description
submitter_id (foreign key to People id)
assigned_to_id (foreign key to People id)
etc

People table with columns:
id
name

models:
class Problem < ActiveRecord::Base
has_many :people ???

class Person < ActiveRecord::Base
has_many :problems

problem_Controller:
def list
@submitter = Person.find(params[:submitter_id] ???

views:problems:list.rhtml
<td><%= problem.submitter.name %></td> ???
 
T

Timothy Goddard

In the future, the rails mailing list would be a better place to post
this. This time, here's a solution:

# problem.rb

class Problem < ActiveRecord::Base
belongs_to :submitter, :class_name => 'Person', :foreign_key =>
'submitted_id'
belongs_to :assigned_to, :class_name => 'Person', :foreign_key =>
'assigned_to_id'
end

# end

look at
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
for full details. You can find the answer to almost every question in
the excellent API documentation available at http://api.rubyonrails.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,293
Latest member
Hue Tran

Latest Threads

Top