`
Hooopo
  • 浏览: 329641 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Dynamic find_by Methods

阅读更多
下面是一个查找未完成的任务的model:
class TaskController < ApplicationController  
    def incomplete  
        @tasks = Task.find(:all, :conditions => ['complete = ?', false])  
    end  
  
    def last_incomplete   
        @task = Task.find(:first, :conditions => ['complete =?', false], :order => 'created_at DESC')  
    end  
end  

用find_by可以更优雅的实现:
class TaskController < ApplicationController  
    def incomplete  
       @tasks = Task.find_all_by_complete(false)    
    end  
  
    def last_incomplete   
        @task = Task.find_by_complete(false, :order => 'created_at DESC')   
    end  
end  



find_by方法带order参数和find方法的一样。

   怎么发论坛来了。。搞错了....................
为什么发到论坛的东西自己删不掉呢?这不合理






分享到:
评论
11 楼 下一站,火星 2009-08-09  
Hooopo 写道
下一站,火星 写道
Hooopo 写道
1.嗯,放在model更好一些:
class Task < ActiveRecord::Base  
  belongs_to :project  
  
  def self.find_incomplete(options = {})  
    with_scope :find => options do  
      find_all_by_complete(false, :order => 'created_at DESC')  
    end  
  end  
end  


2.
引用
Make it Work, Make it Clean, Make it Fast (if necessary)

如果真有性能的需要可以加硬件设备,人家校内网都5000台服务器了http://www.programmer.com.cn/419/
如果有时间优化重构,先优化一下前端
PS:如果性能真的那么重要,像haml这些东西就没有存在的意义了。



系统的瓶颈往往在数据库,你数据库都快累了,你前端搞得在华丽有啥用?另外增加硬件并没有你想象的那么简单,这种server farm往往都需要一个庞大的团队维护,人家校内有几个亿的投资,你怎么不去比?

http://rubyrock.iteye.com/blog/443186


你发这个给我干啥?我早就知道这本书了,so what ?
10 楼 Hooopo 2009-08-09  
下一站,火星 写道
Hooopo 写道
1.嗯,放在model更好一些:
class Task < ActiveRecord::Base  
  belongs_to :project  
  
  def self.find_incomplete(options = {})  
    with_scope :find => options do  
      find_all_by_complete(false, :order => 'created_at DESC')  
    end  
  end  
end  


2.
引用
Make it Work, Make it Clean, Make it Fast (if necessary)

如果真有性能的需要可以加硬件设备,人家校内网都5000台服务器了http://www.programmer.com.cn/419/
如果有时间优化重构,先优化一下前端
PS:如果性能真的那么重要,像haml这些东西就没有存在的意义了。



系统的瓶颈往往在数据库,你数据库都快累了,你前端搞得在华丽有啥用?另外增加硬件并没有你想象的那么简单,这种server farm往往都需要一个庞大的团队维护,人家校内有几个亿的投资,你怎么不去比?

http://rubyrock.iteye.com/blog/443186
9 楼 rainchen 2009-07-31  
关于这种状态型的查询,建议引入状态机,配合一些named_scope小技巧:
http://www.iteye.com/topic/412610?page=2#1081161
实现 Task.completed 这种查询 简单有效又优雅

是的,我在卖瓜
8 楼 Hooopo 2009-07-29  
前端优化又不是美化,和华丽有什么关系?

这是《高性能网站建设指南》一书推荐的前端优化指南:
http://www.douban.com/subject/3132277/
引用


主要包括减少HTTP请求、Edge Computing技术、Expires Header技术、Gzip组件、CSS和JavaScript最佳实践、主页内联、Domain最小化、JavaScript优化、避免重定向的技巧、删除重复JavaScript的技巧、关闭ETags的技巧、Ajax缓存技术和最小化技术等


相信这些还是有点用的.........
7 楼 下一站,火星 2009-07-29  
Hooopo 写道
1.嗯,放在model更好一些:
class Task < ActiveRecord::Base  
  belongs_to :project  
  
  def self.find_incomplete(options = {})  
    with_scope :find => options do  
      find_all_by_complete(false, :order => 'created_at DESC')  
    end  
  end  
end  


2.
引用
Make it Work, Make it Clean, Make it Fast (if necessary)

如果真有性能的需要可以加硬件设备,人家校内网都5000台服务器了http://www.programmer.com.cn/419/
如果有时间优化重构,先优化一下前端
PS:如果性能真的那么重要,像haml这些东西就没有存在的意义了。



系统的瓶颈往往在数据库,你数据库都快累了,你前端搞得在华丽有啥用?另外增加硬件并没有你想象的那么简单,这种server farm往往都需要一个庞大的团队维护,人家校内有几个亿的投资,你怎么不去比?
6 楼 koalant 2009-07-29  
QuakeWang 写道
对于这种代码重构,我个人比较喜欢named_scope


的确楼主的那些重构, rails 后来版本中的 named_scope 是最好的方式,现在自己写这种 condition query method 已经很少了。
5 楼 koalant 2009-07-29  
Hooopo 写道

2.
引用
Make it Work, Make it Clean, Make it Fast (if necessary)

如果真有性能的需要可以加硬件设备,人家校内网都5000台服务器了http://www.programmer.com.cn/419/
如果有时间优化重构,先优化一下前端吧
PS:如果性能真的那么重要,像haml这些东西就没有存在的意义了。



校内网只是个特例,大多数公司的网站不会超过5台服务器,你去看看 robbin 的 twitter, javaeye 也三年了,他现在买台服务器也要考虑再三, 还在不停地想法榨干服务器最后一点优化性能的可能,都说横向扩展,但是实际应用中成本的考虑更多一些。而这里大多数人做网站也许一辈子也不会管理超过三台服务器,所以有的新闻只是新闻,有时候对个人和公司来说连参考价值都没有。

PS: ERB,builder 的性能确实要比 HAML 好一些(现在haml差距也不是很大了), 如果只是做输出 xml 的 service 应用,那么 haml 还真没必要用。
参看:
http://ridingtheclutch.com/2009/07/13/the-ultimate-ruby-performance-test-part-1.html
4 楼 QuakeWang 2009-07-29  
对于这种代码重构,我个人比较喜欢named_scope
class Task < ActiveRecord::Base    
  named_scope :incomplete, :conditions => "complete = false"
  named_scope :latest, :order => "created_at desc"
end

@tasks = Task.incomplete
@tasks = Task.incomplete.latest


我使用Dynamic find比较多的地方在和initialize共用,比如记录用户访问他人博客的功能:
Visit.find_or_initialize_by_user_id_and_visitor_id(@user.id, current_user.id).update_attribute(:visited_at, Time.now)
3 楼 Hooopo 2009-07-29  
1.嗯,放在model更好一些:
class Task < ActiveRecord::Base  
  belongs_to :project  
  
  def self.find_incomplete(options = {})  
    with_scope :find => options do  
      find_all_by_complete(false, :order => 'created_at DESC')  
    end  
  end  
end  


2.
引用
Make it Work, Make it Clean, Make it Fast (if necessary)

如果真有性能的需要可以加硬件设备,人家校内网都5000台服务器了http://www.programmer.com.cn/419/
如果有时间优化重构,先优化一下前端吧
PS:如果性能真的那么重要,像haml这些东西就没有存在的意义了。

2 楼 koalant 2009-07-29  
下一站,火星 写道
引用
def last_incomplete    
      @task = Task.find_by_complete(false,rder => 'created_at DESC')    
end 


感觉你这个其实可以讨论一下
one 这种封装到底有没有用
two 这种作用有多大,有没有过度封装的嫌疑


1:将其封装为一个 helper 方法或者 controller 方法的确没必要,应该是将它移到 Model 中作为 self method,这样这个方法在 association 中也可以用,这样才有意义。

2:动态方法在网站开发初期还是不错的,可以写出简洁易懂的代码,但是到了后期,多半会重构为 :conditions query , 因为 method_missing 毕竟还是有性能损失的。
1 楼 下一站,火星 2009-07-29  
引用
def last_incomplete    
      @task = Task.find_by_complete(false,rder => 'created_at DESC')    
end 


感觉你这个其实可以讨论一下
one 这种封装到底有没有用
two 这种作用有多大,有没有过度封装的嫌疑

相关推荐

    Methods in Medical Informatics: Fundamentals of Healthcare英文

    This book provides an ... Within its chapters, you will find descriptions of the basic methods and implementations needed to complete many of the projects you will encounter in your biomedical career.

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: Added division by zero protect in method TFlexControl.MovePathSegment. - FIX: The background beyond docuemnt wasn't filled when TFlexPanel.DocClipping=True. - FIX: In "Windows ClearType" font ...

    Computer Networks and Internets with Internet Applications (Routing, DHCP)

    Computer Networks and Internets with Internet Applications, 4e By Douglas E. Comer index 41.1 Introduction 41.2 Bootstrapping Protocol Software 41.3 Protocol Parameters 41.4 Protocol ...

    Mastering Python [2016]

    Python is a dynamic programming language. It is known for its high readability and hence it is often the first language learned by new programmers. Python being multi-paradigm, it can be used to ...

    CodeLobster_PHP_Edition_Pro_4.1.0

    methods etc - we've implemented it for you in the autocomplete feature for PHP, HTML, JavaScript and even CSS. Also, you can always get necessary help information by F1 or using special Help control....

    Mastering Python(PACKT,2016)

    Python is a dynamic programming language. It is known for its high readability and hence it is often the first language learned by new programmers. Python being multi-paradigm, it can be used to ...

    acpi控制笔记本风扇转速

    condition was removed by modifying AcpiWalkNamespace to (by default) ignore all temporary namespace entries created during any concurrent control method execution. An additional namespace race ...

    julia-1.1.0-win64

    time type inference (augmented by optional type annotations), and partly because of a strong focus on performance from the inception of the project, Julia's computational efficiency exceeds that of ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to ...

    OpenGL development cookbook

    For reflective objects, such as mirrors and dynamic reflections, render-to-texture functionality using FBO and dynamic cube mapping are detailed. In addition to graphics, image processing techniques ...

    .NET反编译工具ILSpy2.3.0 获得DLL EXE 项目的源代码 好用 免费 最新 最全

    Search for types/methods/properties (substring) Hyperlink-based type/method/property navigation Base/Derived types navigation Navigation history BAML to XAML decompiler Save Assembly as C# Project ...

    CodeLobster.PHP.Edition.Pro.4.3.2

    methods etc - we've implemented it for you in the autocomplete feature for PHP, HTML, JavaScript and even CSS. Also, you can always get necessary help information by F1 or using special Help control....

    CodeLobster PHP Edition Pro 4.0.1

    methods etc - we've implemented it for you in the autocomplete feature for PHP, HTML, JavaScript and even CSS. Also, you can always get necessary help information by F1 or using special Help control....

    ASP.NET Core: Cloud-ready, Enterprise Web Application Development

    ASP.NET Core: Cloud-ready, Enterprise Web Application Development by Mugilan T. S. Ragupathi English | 9 Jun. 2017 | ASIN: B072KDVHCS | 1414 Pages | AZW3 | 36.34 MB Create fast, scalable, and high-...

    微软内部资料-SQL性能优化5

    SQL Server can find all pages belonging to an index or table by examining the IAM pages. Sysindexes contains a pointer to the first IAM page, and each IAM page contains a pointer to the next one. The ...

    微软内部资料-SQL性能优化3

     Discuss dynamic locking and lock escalation.  Differentiate locks, latches, and other SQL Server internal “locking” mechanism such as spinlocks and other synchronization objects. Recommended ...

    JavaScriptORM库Neurosync.zip

     methods: {  finish: function(finished) {  this.$save('finished_at', finished ? Date.now() null);  }  },  dynamic: {  done: function() {  return !this.finished_at;  }  } ...

    Programming Ruby 1.9 & 2.0 The Pragmatic Programmers’ Guide 4th Edition

    including a description of all the standard library modules, a complete reference to all built-in classes and modules (including all the new and changed methods introduced by Ruby 1.9, 1.9.2, 1.9.3, ...

    CakePHP 1.3 Application Development Cookbook.pdf

    model methods. The third and fourth recipes show how to test controller actions, and how to test that our views are showing what we expect. The last recipe shows how to run the test in a non-...

Global site tag (gtag.js) - Google Analytics