Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access my model class inside my module rails?

Here is my module in lib/p_table.rb:

class PTable
    .
    .
    .
    def convertElmentToMass(el)
         @el = Element.find_by_symbol(el)
    end
end

Here is my model if it is any relevance in my app/models/element.rb

class Element < ActiveRecord::Base
end

For some reason when I try to test my convertElementToMass function it says uninitialized constant PTable::Element. It is weird because I should be able to call my model class anywhere right?

Here is my rspec in lib/controllers/module_spec.rb

require 'spec_helper.rb'
require 'p_table'

describe PTable do
    class DummyClass
    end 

    before(:all) do
        @dummy = DummyClass.new
        @dummy.extend PTable
    end

    describe "testf" do
        it "saves the world" do
            expect(@dummy.(["Na", '2'])).to eq(2.34)
        end
    end
end
like image 925
user3904534 Avatar asked Sep 05 '25 03:09

user3904534


1 Answers

Use ::Element instead of Element only and it will work.

By default, the Rails autoloader will try to find the class internally at the module, by including the :: you let the system know you want the Element class at the root module.

like image 126
Maurício Linhares Avatar answered Sep 07 '25 20:09

Maurício Linhares