Reed G. Law
Reed G. Law

Reputation: 3945

Ruby on Rails calling ActiveRecord model from within ActionController plugin module

I've a got a method in ActiveRecord::User:

def create_user_from_json(user)
  @user=User.new(user)
  if @user.save!
    @user.activate!
  end
end

And I'm trying to call it in a plugin's module method. The plugin is json-rpc-1-1. Here is the relevant code:

class ServiceController < ApplicationController
  json_rpc_service :name => 'cme',                        # required
  :id => 'urn:uuid:28e54ac0-f1be-11df-889f-0002a5d5c51b', # required
  :logger => RAILS_DEFAULT_LOGGER                         # optional

  json_rpc_procedure :name => 'userRegister',
  :proc => lambda { |u| ActiveRecord::User.create_user_from_json(u) },
  :summary => 'Adds a user to the database',
  :params => [{:name => 'newUser', :type => 'object'}],
  :return => {:type => 'num'}
end

The problem is the code in the proc. No matter whether I call ActiveRecord::User.create_user_from_json(u) or ::User.create_user_from_json(u) or User.create_user_from_json(u) I just get undefined method 'create_user_from_json'.

What is the proper way to call a User method from the proc?

Upvotes: 0

Views: 418

Answers (1)

Hugo
Hugo

Reputation: 2913

I think this needs to be a class method instead of an instance method, declare it like this:

def self.create_user_from_json(user)
  ...
end

Upvotes: 1

Related Questions