how call method from model to controller in rails 3

Hi,

     i want to know how to use method from model to be used in
controller like

sale.rb model

def list
  # some stuff
end

employee controller

def show
@stamp = Sale.list
end

which shows me following error
undefined method `list'

how to over come this problem?

thanks,
-pab

You are calling list as a class method so you need to define it as a
class method

def self.list
  # some stuff
end

or you need to call it as an instance method

s = Sale.new
@stamp = s.list

or even

@stamp = Sale.new.list

hi,

  still same error comes
   undefined method 'list'

thanks,
-pab

Show us the current code and the error please.

Also please remember to quote the previous reply so we know what you
are replying to.

Colin

hi,

in employee controller

def show
@stamp=Sale.new.list
end

in sale.rb model i am using

def self.list
@list=Sale.find(:all)
return @list
end

its shows following error
undefined method 'list'

thanks,
-pab

Now you have the opposite problem… You are now calling list as an instance method and you have it defined as a class method.

You need to do ONE of the following.

Define it as a class method, and call it as a class method:

def self.list

code

end

@list = Sale.list

Define it as an instance method and call it as an instance method:

def list

code

end

@list = Sale.new.list

However, based on what the list method does, it’s probably best that you do the former and have it be a class method.

@Tim thanks for your help, your idea works fine

thanks,
-pab

Pab wrote in post #1027023:

@Tim thanks for your help, your idea works fine

thanks,
-pab

Thanks u all

Regards

Fahim Babar Patel