Sorry, I made some mistakes when I tried to simplify my project code.
I'm trying to amend the semantics of an existing named_scope when it's
called from an association, not add an additional one. Here's a more
complete example (which I hope I've gotten right):
class Post < ActiveRecord::Base
has_many :category_assignments
has_many :categories, :through => :category_assignments
named_scope :active, :conditions => {:active => true}
end
class Category < ActiveRecord::Base
has_many :category_assignments
has_many :posts, :through => :category_assignments
end
class CategoryAssignment < ActiveRecord::Base
belongs_to :post
belongs_to :category
# has attribute show_in_category which allows
# showing/hiding posts on a per-category basis
end
I want to be able to do this
some_category.posts.active
And get the semantics of the active_posts method below:
class Category < ActiveRecord::Base
has_many :category_assignments
has_many :posts, :through => :category_assignment
def active_posts
posts.active.all :conditions =>
['category_assignment.show_in_category = ?', true]
end
end
So when I call
Post.active
I get the set of all active posts. But when I call
some_category.posts.active
I get the set of all active posts in some_category where
show_in_category is true (like calling my active_posts method, but
using a named_scope instead).
One possibility is to do something like this:
class Category < ActiveRecord::Base
has_many :category_assignments
has_many :posts, :through => :category_assignment
has_many :visible_posts, :class_name => 'Post', :through
=> :category_assignment,
:conditions => ['category_assignment.show_in_category
= ?', true]
end
And then remember to call
some_category.visible_posts.active
This lets me use the named scope instead of a method but the semantics
aren't right. I'm trying to express the notion that active
category.posts are subject to an additional restriction relative to
active posts. I'm not trying to divide _posts_ into two groups. I'm
just trying to restrict _active_ posts within a category.
-Sven