Damjan R. wrote:
This works as expected in sqlite3
select id, title || body as tb from docs where tb like ‘%en%’
But fails in postgres because tb is unknown.
Postgres is pretty finicky about what gets used in where conditions. If
I recall correctly, SQL Server allows you to reference aliases in the
where, but PG doesn’t (unless I’ve been doing something wrong for the
past two years). So to get it work in Postgres, you’d have to repeat the
SELECT fragment in the WHERE:
select id, title || body as tb from docs where title || body like ‘%en%’
So my questions are.
- Is there universal SQL syntax to support this query?
The duplication is probably the best you’re going to get.
- (whispering) Could it be done in rails (AR) syntax?
Well, there are at least two ways to do this via AR. First, you could
use all finder syntax and have the database do all the work. In that
case, you would be doing the same thing, but in AR:
Doc.find(:all, :select => ‘id, title || body as tb’, :conditions =>
“title || body like ‘%en%’”)
Since AR is just building a SQL statement, you run into the same problem
as when you create it yourself.
The second way that I know of would be to pull everything back and
remove the ones you don’t want:
Doc.find(:all, :select => ‘id, title || body as tb’).delete_if { |doc|
!doc.tb.include? ‘en’ }
Or something like that. But that is the less performant option. First,
you have to pull all the records back, then loop through them doing a
test, and perform an array operation at each deletion. The first way
would be better.
- Should I create a view and query the view?
You could try that. In my database life, I’ve not been terribly
impressed with views and have not made much use of them at all. I don’t
know if there are any implications from an AR perspective with views.
That would be something to look into. Though, as long as you are just
querying, I don’t see what problem there would be.
Peace.