Dot notation ordering

How do I order a collection returned when I’ve accessed it from dot
notation? For example using client.products I’d like the return to be
ordered by product.part_number. I’ve tried to do this Ruby side with
something like client.products.sort {|a, b| a.part_number >
b.part_number} and I get an error. Any idea how to deal with this?

Thanks,
-dustin

On 12/13/06, Dustin W. [email protected] wrote:

How do I order a collection returned when I’ve accessed it from dot
notation? For example using client.products I’d like the return to be
ordered by product.part_number. I’ve tried to do this Ruby side with
something like client.products.sort {|a, b| a.part_number >
b.part_number} and I get an error. Any idea how to deal with this?

products = client.products.find( :all, :order => [ ‘part_number’ ] )


Greg D.
http://destiney.com/

FYI Your sort code was almost right:
@products = client.products.sort {|a, b| b.part_number <=>
a.part_number}

would give you the products sorted high to low.

Hi –

On Thu, 14 Dec 2006, jdswift wrote:

FYI Your sort code was almost right:
@products = client.products.sort {|a, b| b.part_number <=>
a.part_number}

would give you the products sorted high to low.

Or the slightly more concise:

@products = client.products.sort_by {|prod| prod.part_number }

David


Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

On 12/13/06, [email protected] [email protected] wrote:

ordered by product.part_number. I’ve tried to do this Ruby side with
would give you the products sorted high to low.

Or the slightly more concise:

@products = client.products.sort_by {|prod| prod.part_number }

Or the slightly more concise:

@products = client.products.sort_by &:part_number

:wink:

On 12/13/06, Jeremy E. [email protected] wrote:

@products = client.products.sort_by &:part_number

Where is this behavior documented?

Thanks,


Greg D.
http://destiney.com/

On 12/14/06, Greg D. [email protected] wrote:

@products = client.products.sort_by &:part_number

Where is this behavior documented?

Nevermind, I found it:

cat /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/symbol.rb

class Symbol

Turns the symbol into a simple proc, which is especially useful

for enumerations. Examples:

# The same as people.collect { |p| p.name }

people.collect(&:name)

# The same as people.select { |p| p.manager? }.collect { |p|

p.salary }

people.select(&:manager?).collect(&:salary)

def to_proc
Proc.new { |obj, *args| obj.send(self, *args) }
end
end


Greg D.
http://destiney.com/

Hi –

On Thu, 14 Dec 2006, Greg D. wrote:

class Symbol
end
end

It’s also in the development version of Ruby (1.9):

$ /usr/local/lib/ruby-cvs/bin/ruby -ve ‘p %w{ a b c }.map(&:upcase)’
ruby 1.9.0 (2006-12-09 patchlevel 0) [i686-linux]
[“A”, “B”, “C”]

David


Q. What’s a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (Ruby for Rails)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

Excellent. Thank you all for the great answers…

-dustin