Logo

Ruby on Rails 两个Model有多种多对多关联

两个Model进行关联在Rails中是很常见的。一对一、多对多和一对多,都非常简单。有时候Model之间会有多种关联,比如某种岗位会有必要的要求,也有一些加分项。这个时候就是两种多对多的关联了,研究了一下Rails的关联机制,可以发现:利用:source:through是可以实现这种需求的。

例如,我们有careereducational_path两个Model,对它们要进行两种多对多的关联,可以这么做:

首先我们创立required_educational_pathsrecommended_educational_paths这两个Model来支持这两种关联。

它们两个的数据结构是一样的:

t.integer "career_id"
t.integer "educational_path_id"

Model中的代码也是相同的:

attr_accessible :career_id, :educational_path_id
belongs_to :career
belongs_to :educational_path

然后在career这个Model中加上

has_many :required_educational_paths
has_many :required_educationals, :source => 'educational_path', 
  :through => :required_educational_paths
attr_accessible :required_educationals, :required_educational_ids

has_many :recommended_educational_paths
has_many :recommended_educationals, :source => 'educational_path', 
  :through => :recommended_educational_paths
attr_accessible :recommended_educationals, :recommended_educational_ids

可以看到我们使用了required_educationalsrecommended_educationals两个虚拟的Model来让career关联educational_path,同时它们通过之前建立的required_educational_pathsrecommended_educational_paths保存关联的数据。

同理,在educational_path这个Model中加上

has_many :required_educational_paths
has_many :required_careers, :source => 'careers', 
  :through => :required_educational_paths
attr_accessible :required_careers, :required_career_ids

has_many :recommended_educational_paths
has_many :recommended_careers, :source => 'careers', 
  :through => :recommended_educational_paths
attr_accessible :recommended_careers, :recommended_career_ids

到此为止整个关联就算是完成了。使用起来也是非常的简单。你可以使用career.required_educationalscareer.recommended_educationals 或者 career.required_educational_idscareer.recommended_educational_ids,同理在educational_path也可以使用这两种不同的关联。

comments powered by Disqus