1

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

2
  • Are you doing an instance of a user before calling it? I mean User.first.update! Commented May 4, 2013 at 17:48
  • How are you calling update!? Commented May 4, 2013 at 17:49

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.