views:

68

answers:

1

I am trying to do this

Version.find(:all, :joins=>"JOIN editions ON versions.edition_id=editions.id JOIN products ON editions.product_id=products.id", :select=>"products.name, versions.name AS what")

but ActiveRecord is not respecting the AS keyword... any ideas?

Edit: Since both fields are called "name" they are colliding, so I only get the last one in the list.

+1  A: 

From the query:

Product has_many editions (just specifying the relationship here)

Edition has_many versions

foo = Version.find(:all, :joins=>"JOIN editions ON versions.edition_id=editions.id JOIN products ON editions.product_id=products.id", :select=>"products.name, versions.name AS what")
puts foo.inspect

This should give you a value:

[#<Version name: "foobar1">, #<Version name: "foobar2">]

foo[0].what # Will print the value of 'what' returned by the query

When I wrote a similar query a similar hierarchy it gave me perfect results. Would you like to share the stack trace if you are getting an error?

EDIT: foo[0].attributes will print {"name" => "foobar1", "what" => ""} Sorry for the typo here. I had meant to say attributes.

Swanand
Yes! I thought that the console would just spit out all the values, but in fact it doesn't.
Yar
+1 and best answer and all that. Thanks.
Yar