ruby-on-rails – DRY范围方法
发布时间:2020-12-17 01:59:40 所属栏目:百科 来源:网络整理
导读:我正在使用 Ruby on Rails 3.0.7,我想干我的(不要重复自己)我的范围方法. 在模型文件中我有: class Articles::Category ActiveRecord::Base scope :article_related_to,lambda { |user| where('articles_categories_article_relationships.user_id = ?',use
我正在使用
Ruby on Rails 3.0.7,我想干我的(不要重复自己)我的范围方法.
在模型文件中我有: 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. ? 解决方法
我能提供的最好的是:
scope :related_to,lambda { |user,context| tbl = context == :article ? :articles_categories_article_relationships : :comments_articles_article_category_relationships where("#{tbl}.user_id = ?",user.id) } 这样就可以像你建议的那样给你@ comment.article_categories.related_to(@ current_user,:article).但我同意马克斯·威廉姆斯的观点.这会不必要地混淆您的代码而没有真正的收益. 如果您真的希望进一步混淆代码,可以这样做: 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,:source => :comment has_many :article_relationships,:class_name => 'Articles::Categories::ArticleRelationship' has_many :articles,:source => :article (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- reactjs – React 0.14的无状态组件将如何提供性能改进,而不
- 正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长
- c# – 在字节数组中查找字节序列
- c# – Wpf-如何在代码中获取普通TextBox的LineHeight?
- ruby-on-rails – omniauth与facebook不兼容制作
- ruby – 完全脱机安装RVM
- ruby-on-rails – Rails – 在没有url helper的情况下在模型
- 在PostgreSQL命令行psql里格式化输出json字段
- ruby-on-rails – 无法绑定到端口80,但在当前shell上运行没
- C#,使用运算符对通过属性获取的值(get)