This project is archived and is in readonly mode.

#6679 new
Emilio Tagua

Query broken in Rails 3 (?)

Reported by Emilio Tagua | April 6th, 2011 @ 03:25 PM | in 3.x

This used to work for me in latest Rails 2.3, but it's broken on Rails 3.

@opinion_polls = @organizer.opinion_polls.all(
  :select => "DISTINCT(opinion_polls.event_id), opinion_polls.name, opinion_polls.id,  opinion_polls.random_key", 
  :joins => :opinion_poll_recipients, 
  :conditions => ["opinion_poll_recipients.preview = false AND opinion_poll_recipients.last_sent_at > ? ", 7.days.ago.beginning_of_day])

Query generated in Rails 2.3:

SELECT DISTINCT(opinion_polls.event_id), opinion_polls.name, opinion_polls.id, opinion_polls.random_key FROM `opinion_polls` INNER JOIN `events` ON `opinion_polls`.event_id = `events`.id INNER JOIN `opinion_poll_recipients` ON opinion_poll_recipients.opinion_poll_id = opinion_polls.id WHERE (opinion_poll_recipients.preview = false AND opinion_poll_recipients.last_sent_at > '2011-03-30 00:00:00' ) AND ((`events`.organizer_id = 2766))

Query generated in Rails 3:

SELECT `opinion_polls`.*, DISTINCT(opinion_polls.event_id), opinion_polls.name, opinion_polls.id, opinion_polls.random_key FROM `opinion_polls` INNER JOIN `opinion_poll_recipients` ON `opinion_poll_recipients`.`opinion_poll_id` = `opinion_polls`.`id` INNER JOIN `events` ON `opinion_polls`.event_id = `events`.id WHERE ((`events`.organizer_id = 6)) AND (opinion_poll_recipients.preview = false AND opinion_poll_recipients.last_sent_at > '2011-03-30 03:00:00' )

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT(opinion_polls.event_id), opinion_polls.name, opinion_polls.id,  opinion' at line 1: SELECT `opinion_polls`.*, DISTINCT(opinion_polls.event_id), opinion_polls.name, opinion_polls.id,  opinion_polls.random_key FROM `opinion_polls` INNER JOIN `opinion_poll_recipients` ON `opinion_poll_recipients`.`opinion_poll_id` = `opinion_polls`.`id` INNER JOIN `events` ON `opinion_polls`.event_id = `events`.id WHERE ((`events`.organizer_id = 6)) AND (opinion_poll_recipients.preview = false AND opinion_poll_recipients.last_sent_at > '2011-03-30 03:00:00' )

The error is because we are asking a DISTINCT field after selecting every field in the table (opinion_polls.*).

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages