Forum: Ruby on Rails using two foreign keys to the same table

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.
Josh K. (Guest)
on 2006-04-06 17:41
i am working on a task manager with ruby on rails. it is my first major
project, so i'm still gettng my feet wet as i learn.

i've been trying to use the built in relationships to link my tables
together, but i'm not able to use the same naming conventions. for each
task, it has a field for the creator, and another field for the
assignee. both of these fields reference the users table. obviously i
can't have two fields that say user_id in my tasks table, so how do i
get them to link right and specify a different field?

here is my current database schema:
_Tasks_
  id int not null auto_increment,
  task_name varchar(100) not null,
  description text not null,
  user_id int not null,
  assignee int not null,
  date_due date not null,
  completed_at datetime not null,
  constraint fk_task_user foreign key (user_id) references users(id),
  constraint fk_task_assignee foreign key (assignee) references
users(id),

_Users_
  id int not null auto_increment,
  username varchar(20) not null,
  password varchar(20) not null,
  full_name varchar(100) not null,
  email varchar(150) not null,
  primary key (id)
Wiebe C. (Guest)
on 2006-04-06 18:32
(Received via mailing list)
Josh K. wrote:

> here is my current database schema:
> users(id),
>
> _Users_
>   id int not null auto_increment,
>   username varchar(20) not null,
>   password varchar(20) not null,
>   full_name varchar(100) not null,
>   email varchar(150) not null,
>   primary key (id)
>

(This is my first reply through the Gmane gateway, let's hope it
works...)

You don't have to name your foreign key user_id. Rails can indeed guess
the
correct relations when you do, but you can override. From the api docs:

belongs_to :author, :class_name => "Person", :foreign_key => "author_id"

http://api.rubyonrails.com/classes/ActiveRecord/As...

You could do something like

belongs_to :user, :foreign_key => "creator_id"
belongs_to :user, :foreign_key => "assignee_id"
Josh K. (Guest)
on 2006-04-06 18:37
> http://api.rubyonrails.com/classes/ActiveRecord/As...
>
> You could do something like
>
> belongs_to :user, :foreign_key => "creator_id"
> belongs_to :user, :foreign_key => "assignee_id"

exactly what i was looking for. thanks.
Jeremy E. (Guest)
on 2006-04-06 22:05
(Received via mailing list)
On 4/6/06, Wiebe C. <removed_email_address@domain.invalid> wrote:
> You could do something like
>
> belongs_to :user, :foreign_key => "creator_id"
> belongs_to :user, :foreign_key => "assignee_id"
>

I doubt that will work.  Try:

belongs_to :creator, :class_name=>'User', :foreign_key => "creator_id"
belongs_to :assignee, :class_name=>'User', :foreign_key => "assignee_id"

Which is also untested, but more likely to work.
Wiebe C. (Guest)
on 2006-04-06 22:17
(Received via mailing list)
Jeremy E. wrote:
> I doubt that will work.  Try:
>
> belongs_to :creator, :class_name=>'User', :foreign_key => "creator_id"
> belongs_to :assignee, :class_name=>'User', :foreign_key => "assignee_id"
>
> Which is also untested, but more likely to work.

Oh yeah, whoops. The association needs a name :)
This topic is locked and can not be replied to.