Rails 2.3.x - 惰性数据库查询

发布于 2024-11-03 11:26:14 字数 865 浏览 6 评论 0原文

我想基于 GET 参数并使用 named_scope 在 ActiveRecord 中构造一个查询。我想我应该链接一些范围并根据 GET 参数可用性添加条件,但我担心这会使数据库过度工作,在添加的每个查询部分上发送一个新查询:

# in model
named_scope :sorted, :order => 'title, author'
named_scope :archived, :conditions => { :is_archived => true }
named_scope :by_date, lambda { |date| { :conditions => [ 'updated_at = ?', date ] } }

# in controller / helper
@articles = Article.sorted.all
@articles = @articles.by_date(params[:date]) if params[:date]
@articles = @articles.archived if params[:archived] == '1'

我想到的另一个选项是构建一个方法链接字符串然后将使用Object#send发送到对象,但是当named_scope接收参数(例如by_date)时,这似乎有点脏并且有些问题。我意识到我可以构造一个查询字符串以与 :conditions => 一起使用...ActiveRecord::Base#find 中,但我想我应该首先尝试使用named_scope 来看看是否可以使用后者进行延迟查询。关于如何使用named_scope来做到这一点并且不让数​​据库被查询轰炸有什么建议吗?谢谢。

I would like to construct a query in ActiveRecord based on GET params and using named_scope. I thought I'd chain some scopes and add conditions based on GET param availability, but I fear this will overwork the db, sending a new query on each query portion added:

# in model
named_scope :sorted, :order => 'title, author'
named_scope :archived, :conditions => { :is_archived => true }
named_scope :by_date, lambda { |date| { :conditions => [ 'updated_at = ?', date ] } }

# in controller / helper
@articles = Article.sorted.all
@articles = @articles.by_date(params[:date]) if params[:date]
@articles = @articles.archived if params[:archived] == '1'

Another option I've thought of is building a method chaining string that'll then be sent to the object using Object#send, but that seems a bit dirty and somewhat problematic when the named_scope receives arguments (like by_date). I realize I can construct a query string to use with :conditions => ... in ActiveRecord::Base#find, but I thought I'd try first with named_scope to see if it's possible to do lazy querying with the latter. Any suggestions on how to do this using the named_scope and not having the database bombarded by queries? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-11-10 11:26:14

你可以让 lambda 更智能

# in model
named_scope :sorted, :order => 'title, author'
named_scope :archived, lambda { |is_archived| (is_archived == 1) ? {:conditions => {:is_archived => true}} : {}}
named_scope :by_date, lambda { |date| date.nil? ? {} : { :conditions => [ 'updated_at = ?', date ]}}

# in controller / helper
@articles = Article.by_date(params[:date]).archived(params[:archived]).sorted

You can make lambda more smarter

# in model
named_scope :sorted, :order => 'title, author'
named_scope :archived, lambda { |is_archived| (is_archived == 1) ? {:conditions => {:is_archived => true}} : {}}
named_scope :by_date, lambda { |date| date.nil? ? {} : { :conditions => [ 'updated_at = ?', date ]}}

# in controller / helper
@articles = Article.by_date(params[:date]).archived(params[:archived]).sorted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文