ActiveRecord犯错误:表“users”缺少FROM子句条目

时间:2012-09-24 18:51:53

标签: ruby-on-rails ruby-on-rails-3 activerecord

我正在使用User.delete_at在我的应用中启用用户软删除功能。设置该字段后,我不希望在整个应用中返回已删除的用户。所以我设置了以下内容,因为我有以下模型:

User.rb
    has_many :groups, :through => :group_members, :conditions => ['banned = ?', false]
    default_scope :conditions => 'users.deleted_at IS NULL'

Group.rb
    belongs_to :user
    has_many :group_members, :dependent => :destroy, :include => :user, :conditions => ['banned = ?', false]

GroupMember.rb
    belongs_to :group
    belongs_to :user
    default_scope joins(:user).where('users.deleted_at IS NULL')

然后问题就是当我跑步时:

current_user.groups

我收到以下错误:

    Group Load (0.5ms)  SELECT "groups".* FROM "groups" INNER JOIN "group_members" ON "groups"."id" = "group_members"."group_id" WHERE "group_members"."user_id" = 1 AND (users.deleted_at IS NULL) AND (banned = 'f')
PG::Error: ERROR:  missing FROM-clause entry for table "users"
LINE 1: ..."group_id" WHERE "group_members"."user_id" = 1 AND (users.dele...

有没有更好的方法来让User.delete_at用户不会在整个应用中返回?感谢

1 个答案:

答案 0 :(得分:0)

我认为你的

.where('users.deleted_at IS NULL')

条件应该是:组模型中has_many的条件......