문제

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'...

도움이 되었습니까?

해결책

you can change

 {where(lesson: {published: true})}

to

 where('lessons.published = true').references(:lesson)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top