postgresql - Rails 4 / Ruby2 / PG9.3: ActiveRecord confuses tables on SQL subselect -
in rails app projects assigned users via assignments. want retrieve unassigned projects specific user. sql query:
select * projects id not in (select project_id assignments user_id = 1
lists unassigned projects user 1 when entered in pgadmin.
here rails code:
class user < activerecord::base has_many :assignments, dependent: :destroy has_many :assigned_projects, through: :assignments, source: :project has_many :unassigned_projects, :class_name => 'project', :finder_sql => proc { ["select * projects id not in (select project_id assignments user_id = ? )", self.id] }
this raises:
activerecord::statementinvalid - pg::error: error: column projects.user_id not exist line 1: select 1 one "projects" "projects"."user_id" = $1
which no surprise, because correct column "assignments.user_id". apparently rails not respect second from. wrong code?
update: inspecting "unassigned_projects"-collectionproxy reveals, contains exactly supposed contain. seems first assembled correctly. however, afterwards pg raises error, when calling:
if @unassigned_projects.any?
but not if calling:
if @assigned_projects.any?
["select * project id not in (select project_id assignements a.user_id = ?)", self.id]
also, shouldn't table named projects
instead of project
? ror uses plural forms table names.
finally, correct spelling assignments
, without 'e'.
Comments
Post a Comment