给定以下两个类:
class VacationRequestDay < ActiveRecord::Base
belongs_to :vacation_request, :inverse_of => :vacation_request_days,
:class_name => "VacationRequest",
:foreign_key => "vacation_request_id"
#attributes: :id, :vacation_request_id, :date, :fullday
end
class VacationRequest < ActiveRecord::Base
has_many :vacation_request_days, dependent: :destroy,
inverse_of: :vacation_request
#attributes: :id, state, :user
end
我想收到所有的 VacationRequestDaysvacation_request_day.vacation_request.state == :pending
我尝试了以下方法(在 VacationRequestDay 类中):
scope :by_state, lambda { |state| joins(:vacation_request).where('vacation_request.state = ?', state) }
which results in the following Postgres error:
ActiveRecord::StatementInvalid:
PG::Error: ERROR: missing FROM-clause entry for table "vacation_request"
LINE 1: ...cation_request_days"."vacation_request_id" WHERE (vacation_r...
^
: SELECT "vacation_request_days".* FROM "vacation_request_days" INNER JOIN "vacation_requests" ON "vacation_requests"."id" = "vacation_request_days"."vacation_request_id" WHERE (vacation_request.state = 'pending')
其次我试过:
def self.by_state(state)
VacationRequestDay.joins(:vacation_request)
.where(:vacation_request => { :state => state })
end
这会导致相同的错误。有谁知道如何解决它?如您所见,我已经尝试了 rails guide和stackoverflow的提示