`
fireflyman
  • 浏览: 113354 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

Keep Finders on Their Own Model

    博客分类:
  • ROR
阅读更多
原文參考:http://rails-bestpractices.com/posts/13-keep-finders-on-their-own-model

不可改變你那壞習慣
class Post < ActiveRecord::Base
  has_many :comments

  def find_valid_comments
    self.comment.find(:all, :conditions => {:is_spam => false},
                :limit => 10)
  end
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

class CommentsController < ApplicationController
  def index
    @comments = @post.find_valid_comments
  end
end

用name_scope重構下-->
重構威武:
class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post

  named_scope :only_valid, :conditions => { :is_spam => false }
  named_scope :limit, lambda { |size| { :limit => size } }
end

class CommentsController < ApplicationController
  def index
    @comments = @post.comments.only_valid.limit(10)
  end
end
分享到:
评论

相关推荐

    张文钿 Rails Best Practices 幻灯片

    * Keep Finders on Their Own Model * Love named_scope * the Law of Demeter * Use Observer Lesson 4. Migration * Isolating Seed Data * Always add DB index Lesson 5. Controller * Use before_...

    finders

    发现者

    ARIA-2.7.5.exe

    200, SICK LMS-100, and Hokuyo URG 04-LX laser-range finders, control of the pan-tilt-zoom camera or pan-tilt unit, Pioneer Gripper and Arm, and more. (Some other devices are supported by separate ...

    nokogiri_bang_finders:将 XML 查找器添加到 Nokogiri,如果未找到任何内容则引发

    nokogiri_bang_finders 这颗宝石说“Nokogiri,如果你找不到我想要的 XML,请大喊大叫。” 例如: doc = Nokogiri :: XML ( "&lt;root&gt;&lt;aliens&gt;&lt;alien&gt;&lt;name&gt;Alf&lt;/name&gt;&lt;/alien&gt;&lt;/aliens&gt;&lt;/root&gt;" ) doc . at ( '...

    finders_keepers:免费项目列表变得容易

    寻找者守护者背景和概述Finders Keepers是一个利用MERN的网络应用程序,该应用程序允许用户发布免费赠品,供其他人领取。 拥有帐户的用户可以创建他们想要免费赠送的物品图片的帖子。 因果用户(没有帐户的用户)将...

    Probabilistic Robotics .pdf

    6.3.3 Mathematical Derivation of the Beam Model 162 6.3.4 Practical Considerations 167 6.3.5 Limitations of the Beam Model 168 6.4 Likelihood Fields for Range Finders 169 6.4.1 Basic Algorithm 169 ...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

    Peter Hlavaty - Back To The CORE (syscan2015)-计算机科学

    CORE Back to thePoC• ACE• Target for ACE• ...- Historical issue- First ROP appear inMSDOS- Widely used asbypass for DEP- Using gadgets- ROP compilers /finders- Depends on preparedstack layouthttp://...

    magento-finder:Magento Finder 扩展了 Symfony Finder 组件并查找 Magento 模块; 模型、控制器、助手等; 重写、cron 作业和事件声明等等

    magento-finder ##注意:此工具目前正在开发中 Magento Finder 扩展了 Symfony Finder 组件并查找特殊的 Magento 文件和目录,这些文件和目录可以是: 模块; 模型、控制器、助手等。 更进一步,它会在模块或...

    bangoo:Bangoo是基于Django的内容管理系统

    班古Bangoo是Django之上的内容管理系统。安装从INSTALLED_APPS及其相关的所有内容(例如: urls.py import)中删除...FINDERS设置为: STATICFILES_FINDERS = ( 'bangoo.theming.staticfiles.finders.FileSystemFind

    mers:猫鼬快速休息服务

    Thebasic idea being you should just define your model/finders and the rest should be be magic.用法安装mers,mongoose,express和body-parser $ npm install express --save $ npm install mongoose --save $...

    django-bower:在Django项目中使用Bower的简单方法

    安装安装django-bower软件包: pip install django-bower 在您的设置中将django-bower添加到INSTALLED_APPS: 'djangobower' , 将staticfinder添加到STATICFILES_FINDERS: 'djangobower.finders.BowerFinder' , ...

    Grails A Quick-Start Guide:Dave Klein (DK)

    4.7 Finishing Up the Domain Model . . . . . . . . . . . . . 71 4.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5 Beyond Scaffolding 75 5.1 Generating Scaffolding Code . . . . . . ...

    graphite-cyanite:一个将石墨网与基于cassandra的Cyanite存储后端一起使用的插件

    api配置文件中: cyanite: urls: - http://cyanite-host:portfinders: - cyanite.CyaniteFinder与石墨网一起使用在石墨的local_settings.py : STORAGE_FINDERS = ( 'cyanite.CyaniteFinder',)CYANITE_URLS = ( '...

    python django 访问静态文件出现404或500错误

    django static文件夹下面的内容方法不了 出现404 500错误 需要查看自己的settings文件确保有一下内容 import os PROJECT_ROOT = os.path.dirname(__... 'django.contrib.staticfiles.finders.FileSystemFinder', 'd

    转录因子和组蛋白修饰的分布特征

    基于染色质免疫共沉淀的二代测序技术得到了大量转录因子结合和组蛋白修饰的ChIP-seq数据,借助于信号峰搜索(Peak finders)算法,ENCODE数据库提供了转录因子结合和组蛋白修饰信号峰(Peaks)数据.利用人类GM12878...

    rna-tools::wrench:rna工具

    rna工具 一个工具箱,用于分析RNA的序列,结构和模拟(还有更多方法!) 在查找其他项目。 最新的 (有关详细说明,请参见 ) 20-10 / 12 :py3包装器并将其包括在... 20-03-21 是一个新工具,用于在创建的Finders

    JWorkBench-开源

    JWorkbench 是一组基于 Java 的 GUI 和非 GUI 框架,允许开发人员轻松构建所需的功能并将其合并到应用程序中。 目前可用的框架包括 Finders、Object Factories 和 Exception Management (m

    slic-android

    SLIC-图像分类MIT ML Modellen auf Android Übersicht 模具IST EIN叉DER 冯献给Android系统。 Diese App benutzt ML Modelle im Tensorflow Lite格式发行人Auswahl ...Einblendung eines Kamera-Finders for genauere

Global site tag (gtag.js) - Google Analytics