【问题标题】:Ruby on Rails - Image has many Tags, activerecordRuby on Rails - 图像有很多标签,activerecord
【发布时间】:2013-07-26 14:55:16
【问题描述】:

使用 rails 4.0.0 我试图了解我应该使用什么“多对多、多对一、一对多等”关系来执行以下操作:

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

【问题讨论】:

  • 请正确格式化您的代码

标签: ruby-on-rails rails-activerecord database-relations


【解决方案1】:

这是最简单的方法——除了

class Image < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :images
end

【讨论】:

    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多