Forum: Ruby on Rails NoMethodError when using find_by_sql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
George P. (Guest)
on 2007-07-09 06:21
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
George P. (Guest)
on 2007-07-09 06:52
Simply Dope wrote:

Sorry, forgot I changed it to just a "find" instead of "find_by_sql".
Error is still the same though.
Rob B. (Guest)
on 2007-07-09 16:26
(Received via mailing list)
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
removed_email_address@domain.invalid
George P. (Guest)
on 2007-07-10 04:52
Thanks Rob, that worked like a champ.

George
This topic is locked and can not be replied to.