komal sharma
komal sharma

Reputation: 195

Getting uninitialized constant while calling class method in rails

I am getting uninitialized constant while accessing other class method from model class

Model class

class Post < ActiveRecord::Base
  before_create :fetch_data
  belongs_to :user
 
  def fetch_data
    self.name = 'test'
    self.get_groups = Groups.new(self.department).verdict
  end
end

Groups class

class Groups
  @@current_state

  def initialize(department_arrangement)
    @@current_state = Marshal.load(Marshal.dump(department_arrangement))
  end
  
  def self.get_groups
    @@current_state.each do |row|
      # code
    end
    return # return some value
  end
  # more code
end

Stack trace

NameError (uninitialized constant Post::Group):
  app/models/post.rb:17:in `fetch_data'
  app/controllers/post_controller.rb:19:in `create'

  Rendered /Users/komal/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (21.1ms)
  Rendered /Users/komal/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (8.7ms)
  Rendered /Users/komal/.rvm/gems/ruby-2.1.4/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms)

Basically I want to pass a parameter self.department coming from views and using that variable I am doing some calculation and saving the result in database. What is the right way to do it? I want to keep get_groups method in group class.

Edit-1

I have added config.autoload_paths += %W( #{config.root}/app/utility ) in my application.rb and require 'group_utility' in model class. Now I can see it's calling the constructor but it is throwing No method exist: get-groups Error.

Upvotes: 0

Views: 5818

Answers (3)

userFriendly
userFriendly

Reputation: 484

You need to "require" the other class and if they are in the same directory use "require_relative".

require_relative 'Groups'

I'm new to rails as well, and from what I have seen this is mostly never done. The extra class would be stored as a helper.

Upvotes: 1

Amit Pal
Amit Pal

Reputation: 11052

NameError (uninitialized constant Post::Group):

What does it mean?

Group class is not accessible or it's not initialized yet. To make it accessible you have to add config.autoload_paths += %W( #{config.root}/app/<name of the directory>) in applicaiton.rb

Now the class will load automatically when you start the server. Adding require '<class-name>' in your model class will make it accessible . As you said you are coming from java background, requuire <class-name> is equivalent to import Groups.

you are all set, Now just need to access the class method and you can do it by following code (mentioned by @tonchis)

self.get_groups = ::Groups.new(self.department).verdict

Upvotes: 0

tonchis
tonchis

Reputation: 648

Ruby namespaces the constants. Here, by calling Groups inside the Post class, it's assuming that it will find the constant within said class.

If you want to refer to a constant declared outside the current namespace, you need to prepend :: to it:

self.get_groups = ::Groups.new(self.department).verdict

If doing this now raises NameError (uninitialized constant Groups): then the file where the Groups class is created is not being loaded.

Upvotes: 3

Related Questions