2013-07-26 28 views
-1

使用rails 4.0.0我想了解什么“多对多,多对一,多对多”等关系我应该用来做以下事情:Ruby on Rails - 图像有很多标签,主动记录

Image Table: 
|file_name|description| 
test.png test image 
test2.jpg another test image 

Tag table: 
|tag_name| 
funny 
creative 
cute 
awesome 


image_tag table: 
|image_id|tag_id| 
1, 1 
1, 2 
1, 3 
2, 2 
2, 4 

etc.. 

我应该使用哪种类型的关系? 这是我使用的考虑:

class Image < ActiveRecord::Base 
    has_and_belongs_to_many :image_tag 
end 

class Tag < ActiveRecord::Base 
    has_and_belongs_to_many :image 
end 
+0

请正确地格式化你的代码 –

回答

1

这是做最简单的方式 - 除了

class Image < ActiveRecord::Base 
    has_and_belongs_to_many :tags 
end 

class Tag < ActiveRecord::Base 
    has_and_belongs_to_many :images 
end