原文參考: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
分享到:
相关推荐
* 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_...
发现者
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 这颗宝石说“Nokogiri,如果你找不到我想要的 XML,请大喊大叫。” 例如: doc = Nokogiri :: XML ( "<root><aliens><alien><name>Alf</name></alien></aliens></root>" ) doc . at ( '...
寻找者守护者背景和概述Finders Keepers是一个利用MERN的网络应用程序,该应用程序允许用户发布免费赠品,供其他人领取。 拥有帐户的用户可以创建他们想要免费赠送的物品图片的帖子。 因果用户(没有帐户的用户)将...
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 ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...
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 文件和目录,这些文件和目录可以是: 模块; 模型、控制器、助手等。 更进一步,它会在模块或...
班古Bangoo是Django之上的内容管理系统。安装从INSTALLED_APPS及其相关的所有内容(例如: urls.py import)中删除...FINDERS设置为: STATICFILES_FINDERS = ( 'bangoo.theming.staticfiles.finders.FileSystemFind
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软件包: pip install django-bower 在您的设置中将django-bower添加到INSTALLED_APPS: 'djangobower' , 将staticfinder添加到STATICFILES_FINDERS: 'djangobower.finders.BowerFinder' , ...
4.7 Finishing Up the Domain Model . . . . . . . . . . . . . 71 4.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5 Beyond Scaffolding 75 5.1 Generating Scaffolding Code . . . . . . ...
api配置文件中: cyanite: urls: - http://cyanite-host:portfinders: - cyanite.CyaniteFinder与石墨网一起使用在石墨的local_settings.py : STORAGE_FINDERS = ( 'cyanite.CyaniteFinder',)CYANITE_URLS = ( '...
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工具 一个工具箱,用于分析RNA的序列,结构和模拟(还有更多方法!) 在查找其他项目。 最新的 (有关详细说明,请参见 ) 20-10 / 12 :py3包装器并将其包括在... 20-03-21 是一个新工具,用于在创建的Finders
JWorkbench 是一组基于 Java 的 GUI 和非 GUI 框架,允许开发人员轻松构建所需的功能并将其合并到应用程序中。 目前可用的框架包括 Finders、Object Factories 和 Exception Management (m
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