Ruby on Rails从ActionController插件模块中调用ActiveRecord模型

时间:2010-11-16 20:39:16

标签: ruby-on-rails ruby

我在ActiveRecord :: User中有一个方法:

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

我试图在插件的模块方法中调用它。该插件为json-rpc-1-1。以下是相关代码:

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

问题是proc中的代码。无论我是拨打ActiveRecord::User.create_user_from_json(u)还是::User.create_user_from_json(u)还是User.create_user_from_json(u),我都会undefined method 'create_user_from_json'

proc调用用户方法的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

我认为这需要是一个类方法而不是实例方法,声明如下:

def self.create_user_from_json(user)
  ...
end