Anujan
Anujan

Reputation: 938

undefined method when calling a method in a model of Rails

I keep getting undefined method for when I call a certain method from my Model.

class User < ActiveRecord::Base
  def update!
     request_info
  end
  def request_info
     return "hmmm"
  end
end

request_info inside of update! is not defined I've tried making it self.request_info as well but that doesn't work either

Upvotes: 1

Views: 4941

Answers (2)

Quazi Marufur Rahman
Quazi Marufur Rahman

Reputation: 2623

There are two ways to call a method in rails.

class Foo
  def self.bar
    puts 'class method'
  end

  def baz
    puts 'instance method'
  end
end

Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class

Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>

Are you doing the same? I have taken this example from here. Take a look at that page for details.

Upvotes: 5

bjelli
bjelli

Reputation: 10070

update! is a bad choice for a method name: update is already defined as a (private) method on ActiveRecord::Base - that might lead to confusion.

>> u = User.last
>> u.update
NoMethodError: private method `update' called for #<User:0x007ff862c9cc48>

but apart from that you code works perfectly fine when I try it in the console:

>> u = User.last
>> u.update!
=> "hmmm"

Upvotes: 0

Related Questions