原文參考:
http://rails-bestpractices.com/posts/1-move-finder-to-named_scope
壞習慣:
下面的代碼看了就讓人感覺不舒服,不但寫的繁雜,而且也不美觀...
class PostsController < ApplicationController
def index
@published_posts = Post.find(:all, :conditions => { :state => 'published' },
:limit => 10,
:order => 'created_at desc')
@draft_posts = Post.find(:all, :conditions => { :state => 'draft' },
:limit => 10,
:order => 'created_at desc')
end
end
我們可以用name_scope來拯救它...
清新一個伶俐一個燦爛一個,大嘴巴子抽Y的:
class PostsController < ApplicationController
def index
@published_posts = Post.published
@draft_posts = Post.draft
end
end
class Post < ActiveRecord::Base
named_scope :published, :conditions => { :state => 'published' },
:limit => 10,
:order => 'created_at desc'
named_scope :draft, :conditions => { :state => 'draft' },
:limit => 10,
:order => 'created_at desc'
end
分享到:
相关推荐
它通过将 named_scope 组合为 SQL 片段来组装查询。 适用于动态构建复杂SQL。设置GongoDB 使用 PDO 扩展连接数据库。 include "gongo.php";$pdo = new PDO("mysql:host=localhost;dbname=dbname", "user", ...
A_survey_of_named_entity_recognition_and_classification
放到lib/python2.7/lib-dynload/下解决Python2的No module named _sqlite3问题
编译安装Python3.7报错ModuleNotFoundError: No module named '_ctypes',所需安装包
问题ImportError: No module named _curses解决资源 问题解决地址 http://blog.csdn.net/qq_19299071/article/details/56013485
named_let named_let可用于使rspec的输出更易于阅读。 它是let的包装函数。 named_let(:name){ obj }将返回'obj#to_s'和'obj#inspect'的值更改为:name,然后将'rspec -format d'的输出消息改进为更具可读性。 ...
python库。 资源全名:named_enum-1.1.0.tar.gz
python3缺少依赖文件,python3有_bz2和bz2,python2只有bz2。把解压文件放在 python安装目录/lib/python3/lib-dynload
python3.7 pandas执行失败,报"no module named '_bz2'"的错误。将资源下载下来,然后放到python3.7相应的目录python3/lib/python3.7/lib-dynload下。再给资源可执行权限:chmod 755 _bz2.cpython-37m-x86_64-linux-...
文字的 有关更多文档,请访问 。 描述: Textacular公开了PostgreSQL全文...您的模型现在可以使用搜索方法: #basic_search方法是您可能期望的:从字面上看您要发送给它的内容,而对输入没有任何幻想: Game . basi
use named semaphore to lock and unlock the resource, like mutex, but it use between processed.
experimental ladder trading system for testing front loading averages
中文命名实体识别数据集本项目尝试使用了多种不同的模型(包括HMM,CRF,Bi-LSTM,Bi-LSTM + CRF)来解决中文命名实体识别问题,数据集用的是论文ACL 2018 中收集的简历数据,数据的格式如下,它的每个行由一个字...
Experimental exponential and hedge trading strategy
fluent-plugin-named_pipe 命名为Fluentd的管道输入/输出插件。输入插件配置< source> type named_pipe path /path/to/file tag foo.bar format ltsv参数小路命名管道的文件路径标签发射标签名称格式输入格式,例如...
该程序将windows命名管道的常用功能封装成一个类,使用时只需调用Connect、Read、Write函数即可实现局域网通信。
训练时提示错误: import pycocotools._mask as _mask ImportError: No module named _mask
dns_bind_named_templates
* Move finder to named_scope * Use model association * Use scope access * Add model virtual attribute * Replace Complex Creation with Factory Method * Move Model Logic into the Model Lesson 2....
named_files_Java:Inicio