Skip to content

Instantly share code, notes, and snippets.

@cdimartino
Created February 12, 2016 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdimartino/95f7ed3ab69960922c8a to your computer and use it in GitHub Desktop.
Save cdimartino/95f7ed3ab69960922c8a to your computer and use it in GitHub Desktop.
Has and Belongs to Many

has_and_belongs_to_many and :through do not go together. Your Entry has_and_belongs_to_many :tags:

class Entry < ActiveRecord::Base
  has_and_belongs_to_many :tags
end

class Tag < ActiveRecord::Base
  has_and_belongs_to_many :entries
end

create_table :entry do |t|
  t.string :content, null: false
end

create_table :tags do |t|
  t.string :name, null: false, uniqueness: true
end

create_table :entries_tags do |t|
  t.belongs_to :entry 
  t.belongs_to :tag
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment