1

Out of interest, i was attempting to rewrite

Model.joins{other_model}.uniq

(which generates):

=> "SELECT DISTINCT [model].* FROM [model] INNER JOIN [other_model] ON [other_model].[model_id] = [model].[id]"

In pure Squeel, however the closest i can get is

Model.joins{other_model}.select{distinct(id)}

Which generates:

=> "SELECT DISTINCT [model].[id] FROM [model] INNER JOIN [other_model] ON [other_model].[model_id] = [model].[id]"

How would i do a DISTINCT [model].* in Squeel? Is it possible?

Thanks

rwb
  • 4,309
  • 8
  • 36
  • 59

2 Answers2

2

You need to quote the * in backticks

Model.select{distinct(`*`)}.to_sql

Produces:

SELECT distinct(*) from `models`
Jeremy
  • 122
  • 1
  • 6
  • While the `to_sql` looks right, actually running the query produces: `ActiveRecord::StatementInvalid: TinyTds::Error: Incorrect syntax near '*'.: EXEC sp_executesql N'SELECT distinct(*) FROM [models]'` – rwb May 16 '13 at 10:47
  • Yeah well that depends on the database. MySQL accepts distinct(*), with postgres you'd need to say `models.*` within the backtick. – Jeremy Jun 08 '13 at 06:11
0

Can't you pass wildcard char instead?

Model.joins{other_model}.select{distinct('*')} 
Pavel S
  • 1,543
  • 8
  • 14
  • Thanks for the reply. Your code produces the following SQL: `SELECT distinct(N'*') FROM [model] INNER JOIN [other_model] ON [other_model].[model_id] = [model].[id]` which just returns `*` – rwb Mar 12 '13 at 18:06