Ruby on rails 干法

Ruby on rails 干法,ruby-on-rails,ruby,ruby-on-rails-3,scope,dry,Ruby On Rails,Ruby,Ruby On Rails 3,Scope,Dry,我使用的是RubyonRails3.0.7,我希望干燥(不要重复)我的scope方法 在模型文件中,我有: class Articles::Category < ActiveRecord::Base scope :article_related_to, lambda { |user| where('articles_categories_article_relationships.user_id = ?', user.id) } scope :comment_related_to,

我使用的是RubyonRails3.0.7,我希望干燥(不要重复)我的scope方法

在模型文件中,我有:

class Articles::Category < ActiveRecord::Base
  scope :article_related_to, lambda { |user| where('articles_categories_article_relationships.user_id = ?', user.id) }
  scope :comment_related_to, lambda { |user| where('comments_articles_article_category_relationships.user_id = ?', user.id) }


  has_many :comment_article_category_relationships
  has_many :comments,
    :class_name  => 'Comments::Articles::ArticleCategoryRelationship',
    :through     => :comment_article_category_relationships,
    :source      => :comment

  has_many :article_relationships
    :class_name  => 'Articles::Categories::ArticleRelationship',
  has_many :articles,
    :through     => :article_relationships,
    :source      => :article
end
如何“干燥”scopes方法以使
:article\u related\u to
:comment\u related\u to
都可以使用以下内容


我能提供的最佳服务如下:

scope :related_to, lambda { |user, context|
  tbl = context == :article ? :articles_categories_article_relationships
                            : :comments_articles_article_category_relationships
  where("#{tbl}.user_id = ?", user.id)
}
这将为您提供与(@current\u user,:article)相关的
@comment.article\u类别。但我同意马克斯·威廉姆斯的观点。这不必要地混淆了您的代码,没有任何实际好处

如果您确实希望进一步混淆代码,可以这样做:

@comment.article_categories.comment_related_to(@current_user)
@comment.article_categories.article_related_to(@current_user)
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
请注意,我相信你的协会有几个拼写错误。可能应该是:

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

呼,我的眼睛在流血,只是想把这东西弄直!无论如何,你可以通过@current_user object获得你想要的东西吗?@dogenpunk-顺便说一句:“我的眼睛在流血,只是想把这东西弄直!”。。。你什么意思在我的例子中,我可以通过'@current_user'对象获得我想要的东西,但在另一方面,我会遇到与问题中描述的问题相同的问题。我个人认为这太枯燥了:可读性比枯燥对我来说更枯燥,这使得代码更难阅读。DRY应该是将功能或值等放在一个地方,而不是最小化代码行。
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