p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Ruby on Rails for Microsoft Developers (http://p2p.wrox.com/book-ruby-rails-microsoft-developers-494/)
-   -   name_scope section page 188 & 189 (http://p2p.wrox.com/book-ruby-rails-microsoft-developers/76260-name_scope-section-page-188-189-a.html)

daveporter September 21st, 2009 09: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 06:45 PM

Hi Dave,

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

Code:

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:

Code:

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:

Code:

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.

Cheers,
Antonio

daveporter October 9th, 2009 04:14 AM

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

Dave


All times are GMT -4. The time now is 12:35 AM.

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