Ryan Florence
Ryan Florence

Reputation: 13483

What's the best way to create linking tables?

Still new to Rails.

I'm certain there's a naming convention for what I'm trying to do here.

You've got posts, and then you've got tags (I'm not actually making a blog but this a scenario we're all familiar with).

I'd set up my tags like this:

ruby script/generate scaffold tag name:string

Then I'd create a linking table like this:

ruby script/generate migration x_tag_post tag_id:integer post_id:integer

So then in the models I'd have xTagAsset

belongs_to :tags
belongs_to :posts

And then in Tags and Posts I'd say

has_many :x_tag_posts

Is that the right way to do this? I have a feeling there's something better.

Upvotes: 1

Views: 3335

Answers (3)

William
William

Reputation: 611

I prefer the has many through association like what Toby Hede wrote.

class Post < ActiveRecord::Base 
  has_many :assets  
  has_many :tags, :through => :assets 
end

class Asset < ActiveRecord::Base 
  belongs_to :post  
  belongs_to :tag
end 

class Tag < ActiveRecord::Base 
  has_many :assets
  has_many :posts, :through => :assets
end 

Hope that will help :)

Upvotes: 1

Toby Hede
Toby Hede

Reputation: 37133

You need to use the built-in Active Record has_and_belongs_to_many or has_many :through => options

HABTM assumes the existence of a table following some basic conventions and lets you use:

class Tags < ActiveRecord::Base 
  has_and_belongs_to_many :assets 
end 

class Asserts < ActiveRecord::Base 
  has_and_belongs_to_many :tags 
end 

Has Many through explicitly declares the joining table:

class Tags < ActiveRecord::Base 
  has_many :assets, :through => "assets_tags"
end 

class Asserts < ActiveRecord::Base 
  has_many :tags, :through => "assets_tags"
end 

The guides I have linked to above have more details on the implementation.

Upvotes: 5

mrdenny
mrdenny

Reputation: 5078

From a purely database perspective you create a table called PostTag which looks something like this. (This is SQL Server syntax, YMMV.)

CREATE TABLE PostTag
(PostId INT,
TagId INT
CONSTRAINT PK_PostTag PRIMARY KEY (PostId, TagId))

Upvotes: 0

Related Questions