Ben Langfeld edited this page Apr 11, 2012 · 6 revisions

DEPRECATION NOTICE: This is old documentation relevant to Adhearsion 1.x and will soon be removed. See the main documentation for up-to-date info.

Active Record is a database object relational mapping (ORM) used created for and used with Ruby on Rails (RoR). Though Adhearsion does not require Active Record for database access, it's by far the most popular option. Database integration in pre-Adhearsion voice applications has always been somewhat of a challenge and Active Record helps push Adhearsion to the top of the voice development frameworks.

The Adhearsion code base from the git repository allows you to integrate an existing RoR project's database configuration and existing Active Record models into your Adhearsion application.

##Integrating with an existing Rails application##

If you already created models for your Rails web application and are looking to use Adhearsion with your existing application, Adhearsion lets you point your project to an existing Rails directory path. When this happens, your Adhearsion code will gain access to your database configurations, your model classes and all of the business logic contained therein. If your Rails app adheres to 'Fat models, thin controllers', then you maximize the reuse of your model classes with Adhearsion.

See the Rails Integration page for further instruction.

##Using Active Record without Rails##

The enable_database option allows you manually connect Active Record to a database. The model files are loaded via the .ahnrc "models" property.

It is not possible to enable_rails and enable_database at the same time. When you enable Rails, it will automatically connect to the same database Rails does and will load the Rails app's models. Doing enable_database does not load any of the Rails framework and instead loads your local models referenced in .ahnrc.

##Choosing a database##

The adapters most battle-tested with Active Record are the sqlite3, PostgreSQL and MySQL adapters, but Adhearsion supports all adapters that ActiveRecord does.

After that, make sure that you set a correct path to the home directory of your application and properly set the environment you want your Adhearsion project to access. Be sure to properly change the environment option if you are testing or staging your Adhearsion application and later plan to use the production settings of your RoR application. The :env option tells Adhearsion which environment settings to use from your RoR config/database.yml file.


By default, Adhearsion will load models from the models/ direction relative to the root of your Adhearsion project. This is specified in the "models" section of the .ahnrc file, a hidden configuration file in the root of your Adhearsion application.

Each file within your models directory should be named after the class it encapsulates. For example, if you have a SupportAgent Active Record class, its filename should be support_agent.rb by convention.

class User < ActiveRecord::Base
  has_many :numbers
  def full_name
    "#{first_name} #{surname}"

##Active Record in the dialplan##

Once you enable either Rails or database integration with Adhearsion, all model classes will be loaded into Adhearsion's Ruby process and will be available for your use.

Below is a common usage example:

internal {
  this_user = User.find_by_extension extension
  dial "SIP/#{this_user.sip_username}"
  voicemail this_user.voicemail_inbox if last_dial_unsuccessful?

##Accessing data##

While this isn't the place for a comprehensive tutorial on databases, ORMs or Active Record, for the sake of consistency a short explanation will be provided here.

Because Ruby is a very dynamic programming language, Active Record will dynamically create column names behind-the-scenes for you on your models. The representation is that the table is a class, while each row is an instantiated object of that class. Active Record also guesses your table name based on the name of your class if you follow Active Record's constraint of table naming (ie - plural table names, etc).

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.