简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM