The best I can offer is this:
scope :related_to, lambda { |user, context| tbl = context == :article ? :articles_categories_article_relationships : :comments_articles_article_category_relationships where("#{tbl}.user_id = ?", user.id) }
This gives you @comment.article_categories.related_to(@current_user, :article) , as you suggested. But I agree with Max Williams. This confuses your code unnecessarily without real gain.
If you really want to confuse your code further, you can do this:
def self.method_missing(method, *args) if method =~ /^(.*)_related_to$/ related_to(*args, $1) else super end end def self.related_to(user, context) through = reflections[context.to_s.pluralize.to_sym].options[:through] tbl = reflections[through].options[:class_name].underscore.pluralize.gsub('/', '_') where("#{tbl}.user_id = ?", user.id) end
Please note that I believe your associations have a couple of typos. Probably should be:
has_many :comment_article_category_relationships, :class_name => 'Comments::Articles::ArticleCategoryRelationship' has_many :comments, :through => :comment_article_category_relationships, :source => :comment has_many :article_relationships, :class_name => 'Articles::Categories::ArticleRelationship' has_many :articles, :through => :article_relationships, :source => :article
source share