you can change
{where(lesson: {published: true})}
to
where('lessons.published = true').references(:lesson)
Question
In my Rails app, enrolled_lessons returns the lessons a user enrolled in. There's a condition in it so it only returns the published lessons. The following code worked in Rails 3.2:
#user.rb
has_many :enrollings, dependent: :destroy
has_many :enrolled_lessons, through: :enrollings, source: :lesson, :conditions => {published: true}
How can I update this code for Rails 4.1? I tried the following:
has_many :enrolled_lessons, -> {where(lesson: {published: true})}, through: :enrollings, source: :lesson
But when I call user.enrolled_lessons, I just get an error:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "lesson" LINE 1: ...N "lessons"."id" = "enrollings"."lesson_id" WHERE "lesson"."... ^ : SELECT "lessons".* FROM "lessons" INNER JOIN "enrollings" ON "lessons"."id" = "enrollings"."lesson_id" WHERE "lesson"."published" = 't'...
Solution
you can change
{where(lesson: {published: true})}
to
where('lessons.published = true').references(:lesson)