Forum: Ruby on Rails Specifying multiple foreign keys to another 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.
Woei S. (Guest)
on 2006-02-04 07:15
(Received via mailing list)
Hi,

By convention, if I have a field that is a reference to another record
in
the `users` table, all I would need to do is to name the column
`user_id`
and RoR would know it is a reference.

However currently I am working on a table that records `actions` that
both
impact on another user, with each action performed by another user. Thus
I'd
have 2 columns like `impacted_user_id` and `actioned_by_user_id`, how do
I
tell raises that these 2 columns are really references to the `users`
table
instead of the `impacted_user` and `actioned_by_user` tables
respectively?

Thanks.
joey__ (Guest)
on 2006-02-04 08:52
You can use the foregin key, key:

  has_many :done_actions, :foreign_key => 'actioned_by_user_id',
:class_name => 'Action'
  has_many :impacted_actions, :foreign_key => 'impacted_user_id',
:class_name => 'Action'

  belongs_to :actioned_by, :foreign_key => 'actioned_by_user_id',
:class_name => 'User'
  belongs_to :impacted, :foreign_key => 'impacted_user_id', :class_name
=> 'User'


Joey
This topic is locked and can not be replied to.