undefined method when calling a method in a model of Rails - ruby-on-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

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.

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"

Related

How can I include the module with class to another class?

I have module with class
module My < Grape::API
prefix 'api'
format :json
class Users
helpers do
include My2 #I want put here method 'some_method'
end
end
end
I have another module
module My2
class Circle
def some_method
"Hello"
end
end
end
I can do this, but I wonder how to do with the class
module My2
def some_method
"Hello"
end
end
I don't understand logical .. Thank you
How can I do it another way?
You can't use include for including a class, because it uses for including methods from modules in classes.
If you want to use a class, try to pass an instance of class or create class's method.
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:0x1e120>
Example
I can put
def some_method(x, y)
My2::Circle.new.some_method(x, y)
end

Why do I get an undefined method when calling model method

I have a model with the following method
class Legacy::Retailer < ActiveRecord::Base
belongs_to :retailers
self.table_name = 'orim_retailers'
def retailer_options
retailers = Array.new
self.display_name.each do |names|
retailers << names
end
end
end
When I try to call this method from the controller as
#retailer_options = Legacy::Retailer.retailer_options
I get a method an undefined method error. I'm not sure what I'm doing wrong.
Because you defined an instance method, not a class method.
To be able to call
Legacy::Retailer.retailer_options
you should define a class method:
def self.retailer_options # note self here
retailers = Array.new
self.display_name.each do |names|
retailers << names
end
end
And if you've meant to call this method on instances, not class itself, you should be able to do the following:
Legacy::Retailer.new.retailer_options
The method retailer_options was defined as an instance method, and you are calling a class method. Take a look at the following code:
class Foo
def self.bar
puts 'class method'
end
def baz
puts 'instance method'
end
end
If you call:
Foo.bar # => "class method"
Foo.baz # => NoMethodError: undefined method ‘baz’ for Foo:Class
And for the instance method:
Foo.new.baz # => instance method
Foo.new.bar # => NoMethodError: undefined method ‘bar’ for #<Foo:0x1e820>
Hope it's more clear now
you've defined an instance method but you're calling it as a class method.
#retailer = Legacy::Retailer.new
#retailer.retailer_options
will work

How does one make a Model perform a method on itself?

class MyAwesomeClass
def foobar
puts "trip!"
end
So that I can perform :
MyAwesomeClass.foobar
=> "trip!"
I keep getting :
NoMethodError: undefined method `foobar' for MyAwesomeClass:Class
class MyAwesomeClass
def self.foobar
puts "trip!"
end
end
Using "self" makes the method a class instance method

Rspec how to set an expectation on the superclass for an overridden method

I've got a model class that overrides update_attributes:
class Foo < ActiveRecord::Base
def update_attributes(attributes)
if super(attributes)
#do some other cool stuff
end
end
end
I'm trying to figure out how to set an expectation and/or stub on the super version of update_attributes to make sure that in the success case the other stuff is done. Also I want to make sure that the super method is actually being called at all.
Here's what I have tried so far (and it didn't work, of course):
describe "#update_attributes override" do
it "calls the base class version" do
parameters = Factory.attributes_for(:foo)
foo = Factory(:foo, :title => "old title")
ActiveRecord::Base.should_receive(:update_attributes).once
foo.update_attributes(parameters)
end
end
This doesn't work, of course:
Failure/Error: ActiveRecord::Base.should_recieve(:update_attributes).once
NoMethodError:
undefined method `should_recieve' for ActiveRecord::Base:Class
Any ideas?
update_attributes is an instance method, not a class method, so you cannot stub it directly on ActiveRecord::Base with rspec-mocks, as far as I know. And I don't think that you should: the use of super is an implementation detail that you shouldn't be coupling your test to. Instead, its better to write examples that specify the behavior you want to achieve. What behavior do you get from using super that you wouldn't get if super wasn't used?
As an example, if this was the code:
class Foo < ActiveRecord::Base
def update_attributes(attributes)
if super(attributes)
MyMailer.deliver_notification_email
end
end
end
...then I think the interesting pertinent behavior is that the email is only delivered if there are no validation errors (since that will cause super to return true rather than false). So, I might spec this behavior like so:
describe Foo do
describe "#update_attributes" do
it 'sends an email when it passes validations' do
record = Foo.new
record.stub(:valid? => true)
MyMailer.should_receive(:deliver_notification_email)
record.update_attributes(:some => 'attribute')
end
it 'does not sent an email when it fails validations' do
record = Foo.new
record.stub(:valid? => false)
MyMailer.should_receive(:deliver_notification_email)
record.update_attributes(:some => 'attribute')
end
end
end
Try replacing should_recieve with should_receive.

Cannot access with_scope from a mixin

I just stumbled over a weird problem, and I don't really understand what is causing this.
In our rails app, let's have a mixin Mixin:
module Mixin
def foo
with_scope :find => ... do
...
end
end
end
which is includeed into a model class elsewhere:
class Model < ActiveRecord::Base
include Mixin
...
end
calling Model.new.foo results in an error: NoMethodError: undefined method with_scope
I then changed the foo method to:
def foo
self.class.with_scope :find => ... do
...
end
end
But this also results in an error: NoMethodError: protected method with_scope called
This seems odd. I would have expected that the mixin methods would behave like any other method in Model. I never stumbled over this before, because all the instance methods like save are there and work as usual.
Am I doing it all wrong?

Resources