Rails find_by - 关联比较

时间:2015-06-08 10:25:15

标签: ruby-on-rails activerecord associations

我有三个型号:

class User < ActiveRecord::Base
    has_many :surveys
end
class Survey < ActiveRecord::Base
    belongs_to :user
    has_many :survey_sessions
end
class SurveySession < ActiveRecord::Base
  belongs_to :survey
end

我的问题:

如果可能的话:

Survey.find_by!(id: params[:id], user: current_user)

为什么这会给我抛出一个SQLException?

SurveySession.joins(:survey)
    .find_by!(id: params[:id], surveys: {user: current_user})


SQLite3::SQLException: no such column: surveys.user: 
SELECT "survey_sessions".* FROM "survey_sessions" INNER JOIN "surveys" ON "surveys"."id" = "survey_sessions"."survey_id" WHERE "survey_sessions"."id" = 3 AND "surveys"."user" = 1 LIMIT 1

提前致谢! :)

1 个答案:

答案 0 :(得分:0)

SurveySession.joins(:survey).find_by!(id: params[:id], surveys: {user_id: current_user})