我试图在rails上连接2个表,但这对我来说变得不可能。
我的schema是:
table "cursos"|
t.string "nombre"
t.integer "user_id"
end
table "users",
t.string "name"
end
user.rb:
class User < ActiveRecord::Base
has_many :cursos
curso.rb
class Curso < ActiveRecord::Base
belongs_to :user
def self.search(nameProf)
(Cursos.joins(:users).where("users.name ilike ?", "%#{nameProf}%").all)
end
它给了我这个错误:
cursoscontroller# index中的NameError未初始化常量Curso::Cursos
谢谢!
试试这个:
def self.search(nameProf)
joins(:users).where("users.name ILIKE ?", "'%#{nameProf}%'")
end
由于search是一个类方法,您可以提交Curso
尝试:
Cursos.select("*").joins(:users).where("users.name ilike ?", "%#{nameProf}%").all