Skip to content

Calling a method of the model

fabrik42 edited this page Apr 20, 2011 · 1 revision

Calling a method of the model

You can easily add any method of a model and its return value to the an api template by just adding the method name as a symbol.

class User < ActiveRecord::Base

  acts_as_api

  api_accessible :only_full_name do |t|
    t.add :full_name
  end

  def full_name
    '' << first_name.to_s << ' ' << last_name.to_s
  end

end

Note: This will only work for public methods of the model.