Search code examples
rubycouchrest

Call class method in Ruby


I am using couchrest_model in a Rails project, and I am trying to retrieve the server URL defined in couchdb.yml.

I can see that server is a class method defined in connection.rb, but how can I access it in code? I am trying:

server = CouchRest::Model.server

but seeing the following error:

NoMethodError (undefined method `server' for CouchRest::Model:Module)

Solution

  • CouchRest::Model is the namespace where CouchRest::Model::Base lives.

    You are expected to create a model by inheriting from Base.

    class Project < CouchRest::Model::Base
      use_database 'projects'
    end
    

    And because Connection is mixed into Model::Base (not Model) then you should be able to access it with

    Project.server
    

    where Project is your model name.

    CouchRest::Model::Base.server
    

    should also work but it's probably not the best solution because it's not model-oriented.