# NoMethodError when using find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897>\
**Category:** Rails\
**Created:** [July 9, 2007, 4:21am UTC](https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897 "2007-07-09T04:21:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [July 9, 2007, 4:21am UTC](https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897/1 "2007-07-09T04:21:46Z")

</div>

I’m try to verify users on login. Here is my code:

def self.authenticate(username,password,account\_code)  
employee = self.find(:all,  
:select =\> “e.id, e.first\_name, e.last\_name, e.username,  
e.account\_id, e.department\_id, o.pay\_type\_id, o.admin\_yn, o.payroll\_yn,  
o.files\_yn, o.dept\_report\_yn,e.salt, e.hashed\_password”,  
:conditions =\> [“e.deleted\_yn=0 and e.username = ? and  
a.account\_code = ?”, username, account\_code],  
:joins =\> "as e left outer join options o on e.id = o.employee\_id  
left outer join accounts a on e.account\_id = a.id ")

```
if employee
  expected_password = encrypted_password(password, employee.salt)
  if employee.hashed_password != expected_password
    employee = nil
  end
end
employee

```

## end

I keep getting an error saying “undefined method ‘salt’”. Any ideas?

Thanks

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [July 9, 2007, 4:52am UTC](https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897/2 "2007-07-09T04:52:57Z")

</div>

Simply Dope wrote:

Sorry, forgot I changed it to just a “find” instead of “find\_by\_sql”.  
Error is still the same though.

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [July 9, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897/3 "2007-07-09T14:26:26Z")

</div>

On Jul 8, 2007, at 10:21 PM, George P. wrote:

> ```
> :joins => "as e left outer join options o on e.id =  
> 
> ```
> 
> ## end
> 
> I keep getting an error saying “undefined method ‘salt’”. Any ideas?
> 
> Thanks

The object refered to by employee is likely not of the Employee model  
class. Try not overriding the :select list in your find and letting  
AR’s associations do some of the heavy lifting:

Assuming associations to options and account:

class Employee \< ActiveRecord::Base  
has\_many :options  
belongs\_to :account

def self.authenticate username, password, account\_code  
if employee = find(:first, :include =\> [:account, :options],  
:conditions =\> [ “employees.deleted\_yn = ?” +  
" AND employees.username = ?" +  
" AND accounts.account\_code  
= ?",  
false, username, account\_code ])  
expected\_password = encrypted\_password(password, employee.salt)  
if employee.hashed\_password != expected\_password  
employee = nil  
end  
end  
employee  
end  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![simplydope](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@simplydope](https://www.ruby-forum.com/u/simplydope)\
**Post date:** [July 10, 2007, 2:52am UTC](https://www.ruby-forum.com/t/nomethoderror-when-using-find-by-sql/108897/4 "2007-07-10T02:52:25Z")

</div>

Thanks Rob, that worked like a champ.

George
