Forum: Ruby Basic Rails Database Foreign Key question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D1fc110cbf58452f943666fe00d63487?d=identicon&s=25 unknown (Guest)
on 2006-02-03 02:41
(Received via mailing list)
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> ???
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-02-03 03:05
(Received via mailing list)
portal33@gmail.com wrote:
> Hi, I'm still waiting on my Agile Rails book to arrive - in the
> meantime ...

You'll likely get better information from the Rails mailing list

http://lists.rubyonrails.org/
Be223e60c56535a0e465b84243aeb0d1?d=identicon&s=25 Timothy Goddard (Guest)
on 2006-02-03 07:39
(Received via mailing list)
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/As...
for full details. You can find the answer to almost every question in
the excellent API documentation available at http://api.rubyonrails.org
This topic is locked and can not be replied to.