1

在这里(http://allaboutruby.wordpress.com/2009/08/08/5-minute-project-in-rails/)我们可以看到如何在表之间创建 1-m 关系,但是我应该执行哪些步骤来创建例如下一种表之间的关系:

http://docs.oracle.com/cd/E14373_01/appdev.32/e13363/issue_track_obj.htm

为了简化,只是如何创建关系user can have several created by him bugs and can be assigned to the several bugs created by other users.

谢谢。

4

1 回答 1

2

首先,您需要 bugs 表上的两列,creator_id 和 assignee_id。然后,您只需创建以下关系:

class User < ActiveRecord::Base
  has_many :created_bugs, :class_name => 'Bug', :foreign_key => :creator_id
  has_many :assigned_bugs, :class_name => 'Bug', :foreign_key => :assignee_id
end

class Bug < ActiveRecord::Base
  belongs_to :creator, :class_name => 'User'
  belongs_to :assignee, :class_name => 'User'
end
于 2013-09-11T20:39:13.523 回答