0

I am building a Rails 5.0 API and trying to have a print_it class method that runs as_json on an object. (I need a separate method to put complex logic into later)

Whenever I test it, it errors with:

NoMethodError (undefined method print_it for #<Class:0x007f7b7b092f20>):

In Model: project.rb

class Project < ApplicationRecord
      def print_it
        self.as_json
      end
end

In controller: projects_controller.rb

class Api::V1::ProjectsController < Api::ApiController
    def index
        render json: Project.print_it
    end
end

How can I use print_it on an object?

1 Answer 1

2
Project.print_it

is calling print_it on the class Project. But, you define print_it as an instance method, not a class method, here:

class Project < ApplicationRecord
  def print_it
    self.as_json
  end
end

You probably want something more like:

class Api::V1::ProjectsController < Api::ApiController
  def index
    render json: @project.print_it
  end
end

Naturally, you'll need to set @project.

To use print_it on an ActiveRecord_Relation called @projects, you could do something like:

@projects.map{|p| p.print_it}

You'll end up with an array.

But that might be expensive, depending on the number of projects and the nature of print_it.

How can I use print_it on an object?

You are 'using' (calling) print_it on an object. Project is an object. Just like @project is an object. You just happen to be calling print_it on an object that doesn't have print_it defined (thus the undefined method error).

I will also note that Jörg W Mittag wishes to say:

I am one of those Ruby Purists who likes to point out that there is no such thing as a class method in Ruby. I am perfectly fine, though, with using the term class method colloquially, as long as it is fully understood by all parties that it is a colloquial usage. In other words, if you know that there is no such thing as a class method and that the term "class method" is just short for "instance method of the singleton class of an object that is an instance of Class", then there is no problem. But otherwise, I have only seen it obstruct understanding.

Let it be fully understood by all parties that the term class method is used above in its colloquial sense.

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

6 Comments

When I use @projects.print it, it returns NoMethodError (undefined method print_it for #<Project::ActiveRecord_Relation:0x007f9bc9398930>)
@projects is a collection (more specifically, an ActiveRecord_Relation, as indicated). You define print_it on an instance of Project. So, you can't call print_it on the collection. Which is what the error is telling you.
So, how would I print_it a collection. The only way I've found so far is making it an instance method print_it(@projects) - is that a proper way of achieving the result?
Updated with answer.
That works, but yes it is very expensive. I think print_it(@projects) should be less expensive as it uses a custom as_json parser we've installed..
|

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.