在表之间创建复杂关系的步骤

时间:2013-09-11 20:11:29

标签: ruby-on-rails scaffolding

这里(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.

的关系

感谢。

1 个答案:

答案 0 :(得分:2)

首先,你需要在bug表上创建两列,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