From a07338d6ec14158b3ce4482c3b64213e600157db Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Mon, 31 Dec 2007 13:51:49 +0000 Subject: [PATCH] ActionItem132: adding HABTM relationship between articles and categories. Declared only in articles by now, though. --- app/models/article.rb | 2 ++ db/migrate/021_add_articles_categories.rb | 14 ++++++++++++++ test/unit/article_test.rb | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 0 deletions(-) create mode 100644 db/migrate/021_add_articles_categories.rb diff --git a/app/models/article.rb b/app/models/article.rb index b8d36e7..d049855 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -9,6 +9,8 @@ class Article < ActiveRecord::Base has_many :comments + has_and_belongs_to_many :categories + acts_as_taggable N_('Tag list') diff --git a/db/migrate/021_add_articles_categories.rb b/db/migrate/021_add_articles_categories.rb new file mode 100644 index 0000000..a8c99ec --- /dev/null +++ b/db/migrate/021_add_articles_categories.rb @@ -0,0 +1,14 @@ +class AddArticlesCategories < ActiveRecord::Migration + def self.up + create_table :articles_categories do |t| + t.column :article_id, :integer + t.column :category_id, :integer + end + add_index(:articles_categories, :article_id) + add_index(:articles_categories, :category_id) + end + + def self.down + drop_table :articles_categories + end +end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index d34cb3a..6162afe 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -171,4 +171,22 @@ class ArticleTest < Test::Unit::TestCase assert_equal(profile.url + "/myarticle", article.url) end + should 'associate with categories' do + env = Environment.default + c1 = env.categories.build(:name => "test category 1"); c1.save! + c2 = env.categories.build(:name => "test category 2"); c2.save! + + article = profile.articles.build(:name => 'withcategories') + article.save! + + assert_raise ActiveRecord::AssociationTypeMismatch do + article.categories << 1 + end + + assert_nothing_raised do + article.categories << c1 + article.categories << c2 + end + end + end -- libgit2 0.21.2