SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Another SQL problem
-
Apr 17, 2006, 05:24 #1
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Another SQL problem
Hi,
I'm fighting to find how to translate alias column name into Rails:
PHP Code:SELECT * FROM properties as p
PHP Code:find(:all,
:select => 'properties.* as p',
:other conditions here
-
Apr 17, 2006, 07:15 #2
- Join Date
- Jun 2004
- Location
- California
- Posts
- 440
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I may be wrong but I don't think you can do something.* as p you have to be specific with the column name such as something.name as p. I have never tried aliases with rails so I'm not sure how they work with ActiveRecord, but I have done them in queries in the past with other languages and I don't think you can do something.*
-
Apr 17, 2006, 08:35 #3
Its been a while since I've written raw SQL but I think the problem here is with your SQL, rather than Rails. You are trying to alias a column, so your SQL doesn't make sense. For example, if you wanted to alias a column you would do something like:
PHP Code:SELECT address as a FROM properties
As for column aliases, I'm not sure Rails automatically creates model properites from column aliases - is this what you are trying to do? (but again, I'm not sure I can see why you'd want to)
-
Apr 17, 2006, 09:03 #4
- Join Date
- Jul 2002
- Location
- Toronto, Canada
- Posts
- 39,347
- Mentioned
- 63 Post(s)
- Tagged
- 3 Thread(s)
yes, you can do something.*
example: select a.*, b.* from a inner join b ... inner join c ...
in this example, you want all columns from tables a and b, but none from c
but something.* as p is wrong, because * cannot be given an alias
-
Apr 17, 2006, 09:04 #5
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
The SQL you've posted creates a table alias and I'm not really sure why you'd want to do that in Rails
PHP Code:select p.id, p.name, p.ref, p.xxx
PHP Code:select properties.id, properties.name, properties.ref, properties.xxx
PHP Code::joins => 'LEFT JOIN properties as p ON p.city_id = cities.id',
-
Apr 17, 2006, 12:14 #6
Originally Posted by HenriIV
PHP Code:select p.* from properties as p
PHP Code::select=>"select properties.* as p"
Erh
-
Apr 17, 2006, 12:44 #7
- Join Date
- Jun 2004
- Location
- France
- Posts
- 129
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks, I just needed another cup of coffee!
it's ok with :from => 'properties as p' http://railsmanual.org/class/ActiveRecord::Base API didn't mention :from so it took me a while to understand
-
Apr 17, 2006, 13:52 #8
I didn't realize there was a :from either. That should come in real handy.
Erh
Bookmarks