我想显示已将帐户与我们的付款购买者提供商相关联的教师列表。当教师连接其付款帐户时,它会更新用户模型上的付款uid字段。
我们的模型设置如下:
class User < ActiveRecord::Base
has_one :teacher
class Teacher
belongs_to :user
我的教师控制员:
class TeachersController < ApplicationController
def index
@teachers = Teacher.order('created_at DESC')
end
我想要显示如下内容:
@teachers = Teacher.where(self.user.uid.present?).order('created_at DESC')
但是我为TeachersController'
返回了一个NoMethodError'undefined方法`user'答案 0 :(得分:1)
尝试:
@teachers = Teacher.joins(:user).where.not('users.uid = ?',nil).order(created_at: :desc)
或强>
valid_users = User.where.not(uid: nil).pluck(:id) # return array of all user's id which has not uid nil
@teachers = Teacher.where(user_id: valid_users).order(created_at: :desc)
答案 1 :(得分:0)
使用范围方法:
class Teacher < ActiveRecord::Base
...
def self.connected
includes(:user).where.not(users: { uid: nil }).references(:user)
end
...
end
您将使用它:
@teacher = Teacher.connected.order('created_at DESC')
答案 2 :(得分:0)
假设您想要拥有用户的教师?
{{1}}