p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Ruby on Rails for Microsoft Developers (http://p2p.wrox.com/forumdisplay.php?f=494)
-   -   name_scope section page 188 & 189 (http://p2p.wrox.com/showthread.php?t=76260)

daveporter September 21st, 2009 08:41 AM

name_scope section page 188 & 189
Hi Everyone,

I'm wondering if anyone can explain the change in @articles across pages 188 and 189 where the :conditions is introduced and the :published => true becomes published = ? AND etc....

I don't understand the code here and would appreciate an explanation...
More specifically why has this changed from :published => true to published => ?
( no colon, becomes a questions mark )
Also why is this section in square brackets where the previous version is using curly brackets.

regards, Dave Porter

Antonio Cangiano October 8th, 2009 05:45 PM

Hi Dave,

in that section I employed two different ways of specifying conditions. The simplest one is to use a hash:


Article.find(:all, :conditions => { :published => true }, :order => "published_at DESC")
This can also be expressed as an array whose first element is a string containing the parameterized condition. For example, the line above could be rewritten as follows:


Article.find(:all, :conditions => ["published = ?", true], :order => "published_at DESC")
Using an array is more flexible than a hash, because you can specify the condition however you wish. The hash only allows you to specify an exact value to match one or more columns.

In fact, later in the same section of the book I used:


Article.find(:all, :conditions => [“published = ? AND published_at <= ?”, true, Time.now.utc], :order => “published_at DESC”)
Notice how published_at is compared with <=. You couldn't express this with the hash syntax.

I hope this helps clarify the issue for you. If it doesn't, please don't hesitate to ask further questions.


daveporter October 9th, 2009 03:14 AM

Thanks Antonio - that all makes sense now, thanks for your response...


All times are GMT -4. The time now is 08:09 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.