Undefined method `current' for Personnel:Class

I can’t quite figure out how I can make this work properly.

I’m in my login_controller.rb

If I use
cond = EZ::Where::Condition.new
cond = “term_date IS NULL”.to_c + c{employment_type === [‘CLIENT’,
‘CSW’, ‘F/T’,‘P/T’, ‘INT’, ‘PRN’]}
@personnel = Personnel.find(:all,
:conditions => cond.to_sql,
:order => ‘last_name, first_name’)

This works but is not very dry since I use this in multiple places.

So in my personnel.rb model, I have something identical…
def current
cond = EZ::Where::Condition.new
cond = “term_date IS NULL”.to_c + c{employment_type === [‘CLIENT’,
‘CSW’, ‘F/T’,‘P/T’, ‘INT’, ‘PRN’]}
Personnel.find(:all,
:conditions => cond.to_sql,
:order => ‘last_name, first_name’)
end

but if I try to use
@personnel = Personnel.current

in my login_controller.rb, I get the 'undefined method ‘current’ for
Personnel:Class error - even if I 'require “personnel” in my login.rb
model (which I wouldn’t think necessary since login.rb
‘has_one :personnel’)

Why doesn’t the definition in the personnel model work in another
controller?

Craig

you’re trying to call an instance method as a class method

make it a class method

def self.current

end

Chris

Hello Craig,
Surely you mean ‘def Personnel.current’ since your using it as a
class method … no ?

Regards
Stef
@personnel = Personnel.current

You’ve defined current as an instance method, but are trying to access
it as
a class method. One way to define it as a class method is to:

def self.current

end

then Personnel.current will be “seen”.

On a side note, you may want to use Personnel.find(:first) as
Personnel.find(:all)
will return an array and I think you just want a single personnel
instance.


Andrew S.

On Fri, 2006-09-01 at 12:22 -0400, Andrew S. wrote:

then Personnel.current will be “seen”.


it’s sometimes amazing that I have gotten as much done as I have - even
after reading/re-reading AWDWR and R4R :wink:

On a side note, you may want to use Personnel.find(:first) as
Personnel.find(:all) will return an array and I think you just want a
single personnel instance.


No, in this instance, I needed a list of only ‘current’ personnel (i.e.
termination date is NULL and employee types and not volunteers/board
members, etc.

Thanks, self.current was indeed the solution.

Craig

I see - said the blind man

thanks - it works

Craig