Question

Using rails 4.0.0 I'm trying to understand what "many to many, many to one, one to many etc" relationship i should use to do the following:

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..

What type of relationship should i use? This is what I'm considering using:

class Image < ActiveRecord::Base
    has_and_belongs_to_many :image_tag
end

class Tag < ActiveRecord::Base
    has_and_belongs_to_many :image
end
Was it helpful?

Solution

That's the simplest way to do it - except

class Image < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :images
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top