Permalink
Browse files

Verify that creating a has_many through record where there is a defau…

…lt_scope on the join model works correctly (creates the join record with the default scope applied)
  • Loading branch information...
1 parent eba7664 commit 491ce5b6ce1a3af42d02efff9a2e42b1a8980553 @jonleighton jonleighton committed with vijaydev Dec 12, 2010
@@ -17,6 +17,8 @@
require 'models/subscriber'
require 'models/book'
require 'models/subscription'
+require 'models/categorization'
+require 'models/category'
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors,
@@ -466,4 +468,9 @@ def test_size_of_through_association_should_increase_correctly_when_has_many_ass
def test_has_many_through_with_default_scope_on_join_model
assert_equal posts(:welcome).comments, authors(:david).comments_on_first_posts
end
+
+ def test_create_has_many_through_with_default_scope_on_join_model
+ category = authors(:david).special_categories.create(:name => "Foo")
+ assert_equal 1, category.categorizations.where(:special => true).count
+ end
end
@@ -77,6 +77,10 @@ def testing_proxy_target
has_many :categorizations
has_many :categories, :through => :categorizations
+ has_many :special_categorizations
+ has_many :special_categories, :through => :special_categorizations, :source => :category
+ has_one :special_category, :through => :special_categorizations, :source => :category
+
has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }
has_many :categorized_posts, :through => :categorizations, :source => :post
@@ -2,4 +2,13 @@ class Categorization < ActiveRecord::Base
belongs_to :post
belongs_to :category
belongs_to :author
-end
+end
+
+class SpecialCategorization < ActiveRecord::Base
+ self.table_name = 'categorizations'
+
+ default_scope where(:special => true)
+
+ belongs_to :author
+ belongs_to :category
+end
@@ -112,6 +112,7 @@ def create_table(*args, &block)
t.column :category_id, :integer
t.column :post_id, :integer
t.column :author_id, :integer
+ t.column :special, :boolean
end
create_table :citations, :force => true do |t|

0 comments on commit 491ce5b

Please sign in to comment.