我正在使用jRuby 1.7.16.1
在Rails 4.2.1中编写应用程序我有用户和项目模型。项目通过加入模型有一个领导者和许多用户:
class Project < ActiveRecord::Base
has_many :users_projects_join
has_many :users, :through => :users_projects_join
has_many :active_users,
-> { where(users_projects_join: {user_status: true}) },
:through => :users_projects_join,
:source => :user
belongs_to :leader, :class_name => 'User', :foreign_key => :leader_id
end
我想拥有:active_users所以我写了上面显示的查询,但它给了我
ERROR: missing FROM-clause entry for table "users_projects_join"
Position: 167: SELECT "users".* FROM "users"
INNER JOIN "users_projects_joins"
ON "users"."id" = "users_projects_joins"."user_id"
WHERE "users_projects_joins"."project_id" = 20
AND "users_projects_join"."user_status" = 't'
但是当我在Postgres中严格执行这个查询时,它给了我所有用户user_status为true。我读到我需要添加连接(:users_projects_join).where(...)但它没有帮助。
答案 0 :(得分:2)
立即尝试:
class Project < ActiveRecord::Base
has_many :users_projects_joins
has_many :users, :through => :users_projects_joins
has_many :active_users,
-> { where(users_projects_joins: {user_status: true}) },
:through => :users_projects_joins,
:source => :user
belongs_to :leader, :class_name => 'User', :foreign_key => :leader_id
end