Getting error when trying to pass an optional argument to a rails model method. My model is like below
Class Antigen
has_and_belongs_to_many :projects
I want to do
antigen = Antigen.first
antigen.projects(injection)
or even
antigen.projects
def projects(injection=nil)
if injection == nil
return projects
else
..do something with injection and then pass projects
end
end
Why is this not working
Instead of calling projects (which you have just overriden) use super:
def projects(injection=nil)
if injection == nil
return super()
else
..do something with injection and then pass projects
end
end
I would refactor it slightly to get rid of one level of abstraction:
def projects(injection=nil)
return super() unless injection
#..do something with injection and then pass projects
end
UPDATE:
Since you're using rails 2.3.8 super cannot be used to access association metho. Instead try:
class Model < AR::Base
has_and_belongs_to_many :proxy_projects, class_name: 'Projects'
private :proxy_projects
def projects(injection=nil)
return proxy_projects unless injection
#..do something with injection and then pass projects
end
end