4
我使用on Rails的Ruby的3.0.7,我想变干(不要重复自己)我的范围的方法。DRY范围方法
在模型文件我有:
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
通过使用上面的代码,我可以这样做:
@comment.article_categories.comment_related_to(@current_user)
@comment.article_categories.article_related_to(@current_user)
我如何“干”作用域,以使可能的方法均使用:article_related_to
和:comment_related_to
以使用类似以下的内容
@comment.article_categories.related_to(@current_user)
# In order to pass the correct "context" 'article' or 'comment' I thought
# something like
#
# @comment.article_categories.related_to(@current_user, 'article')
# @comment.article_categories.related_to(@current_user, 'comment')
#
# but, maybe, there is a way to retrieve automatically that "context" so to
# write only one "DRYed" scope method.
?
吆,我的眼睛都在流血只是试图让这件事直!无论如何,你可以通过@current_user对象获得你正在寻找的东西吗? – dogenpunk
@dogenpunk - 顺便说一句:“我的眼睛在流血只是试图让这件事直” ......你什么意思? - 在我的情况下,我能得到什么,我在寻找通过“@current_user”对象,但是,在这另一面,如问题描述我有同样的问题。 – Backo
我认为这是一个干太远个人:可读性为我拍干,这使得代码更难阅读。 DRY应该是关于保持功能或值等在一个地方,而不是减少的代码行。 –