On Mysql it works correctly.
PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list LINE 1: ) ORDER BY programs.rating DESC, program_sc... ^ :
Query:
SELECT DISTINCT "programs".* FROM "programs" INNER JOIN "program_schedules" ON "program_schedules"."program_id" = "programs"."id" WHERE (programs.rating >= 5 AND program_schedules.start >= '2012-11-03 23:14:43.457659') AND (ptype = 'movie') ORDER BY programs.rating DESC, program_schedules.start DESC
Rails code:
@data = Program.joins(:program_schedules).where('programs.rating >= ? AND program_schedules.start >= ?',5, Time.now).order('programs.rating DESC, program_schedules.start DESC').uniq
I have tried with
Program.select("programs.*, program_schedules.*).joins(:program_schedules).where(...
but, in this way, when I'm going to read
@data.program_schedules
I get a nil value (When I know there are no nil values)
PostgreSQL 9.2 (Heroku), Ruby 1.9.2
Some info about my DB:
class Program < ActiveRecord::Base
has_many :program_schedules
end
class ProgramSchedule < ActiveRecord::Base
belongs_to :program
end
Schema.db
create_table "program_schedules", :force => true do |t|
t.integer "program_id"
t.datetime "start"
end
create_table "programs", :force => true do |t|
t.string "title",
t.string "ptype"
end
EDIT: I don't need to order "programs_schedules" because I need all programs_schedules in my array related to that program.
find_by_sql
– n_i_c_k Nov 3 '12 at 23:39ptype
is from. – Erwin Brandstetter Nov 4 '12 at 0:07