2016-09-19 56 views
0

我尝试加入2代表在轨道上,但其成为IMPOSIBLE给我。错误连接中的Rails

我的模式是:

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 

它给我这个错误:

NameError in CursosController#index uninitialized constant Curso::Cursos

谢谢!

+0

你的类被称为'Curso'所以将其更改为'CURSO .joins(:users)...' –

+1

尝试以单数形式编写Curso。 – Navin

+1

我试过了,它给我下面的错误:NoMethodError在CursosController#指数# 未定义的方法'叫” hernan

回答

0

试试这个:

def self.search(nameProf) 
    joins(:users).where("users.name ILIKE ?", "'%#{nameProf}%'") 
end 

由于搜索是一个类的方法,你可以ommit的Curso

+0

ActiveRecord :: ConfigurationError在Curso上找不到;也许你拼错了吗? 我认为这些表格没有被征服,难道是这样吗? – hernan

+0

它应该是'连接(:用户)',单数。 –

0

尝试:

Cursos.select("*").joins(:users).where("users.name ilike ?", "%#{nameProf}%").all 
+0

与Cursos和Curso相同的错误 – hernan