Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 基于ActiveModel的类不会创建与ActiveRecord等效的结果相同的结果_Ruby On Rails_Activerecord_Hash_Ruby On Rails 3_Activemodel - Fatal编程技术网

Ruby on rails 基于ActiveModel的类不会创建与ActiveRecord等效的结果相同的结果

Ruby on rails 基于ActiveModel的类不会创建与ActiveRecord等效的结果相同的结果,ruby-on-rails,activerecord,hash,ruby-on-rails-3,activemodel,Ruby On Rails,Activerecord,Hash,Ruby On Rails 3,Activemodel,我正在开发一个Rails 3应用程序,容量很大。我使用savon_模型和ActiveModel生成与ActiveRecord等价物类似的行为。下面是我的代码: class TestClass include Savon::Model include ActiveModel::Validations # Configuration endpoint "http://localhost:8080/app/TestService" namespace "http://wsns.t

我正在开发一个Rails 3应用程序,容量很大。我使用savon_模型和ActiveModel生成与ActiveRecord等价物类似的行为。下面是我的代码:

class TestClass
  include Savon::Model
  include ActiveModel::Validations

  # Configuration
  endpoint "http://localhost:8080/app/TestService"
  namespace "http://wsns.test.com/"

  actions :getObjectById, :getAllObjects

  attr_accessor :id, :name

  def initialize(hash)
    @id = hash[:id]
    @name = hash[:name]
  end

  client do
    http.headers["Pragma"] = "no-cache"
  end

  def self.all
    h = getAllObjects(nil).to_array
    return convert_array_hash_to_obj(h, :get_all_objects_response)
  end

  def self.find(id)
    h = getObjectById(:arg0 => id).to_hash
    return convert_hash_to_obj(h, :get_object_by_id_response)
  end

private

  def self.convert_array_hash_to_obj(arrayhash, returnlabel)
    results = Array.new

    arrayhash.each do |hash|
      results << convert_hash_to_obj(hash, returnlabel)
    end

    return results

  end

  def self.convert_hash_to_obj(hash, returnlabel)
    return TestClass.new(hash[returnlabel][:return])
  end

end
而不是

/testclasses/1
因此,我将对象(散列?)打印到控制台以比较输出

[#<System:0xa814cb4 @id="1", @name="CIS">]
[#]
而不是我认为它应该是

[#<System id="1", name="CIS">]
[#]
我有三个问题:
1:打印出来时,我的类名上的十六进制后缀是什么
2:打印到控制台时,如何修改类以匹配所需的输出?
3:为什么前端链接(显示、编辑、删除)会断开?是否有简单的修复方法?

非常感谢您抽出时间,并为垃圾代码/愚蠢的问题道歉。这是我的第一个Ruby或Rails应用程序

加雷斯

  • 十六进制后缀是
    System
  • 您可以通过实现
    inspect
    实例方法在控制台上操作输出
  • Rails url帮助程序使用
    to_param
    实例方法来构建这些链接。如果要将类用作ActiveRecord替代品,则应实现此功能
  • 一般来说,如果您希望将所有Rails产品与模型类的自己实现一起使用,那么应该使用
    ActiveModel:Lint::Test
    来验证ActiveModelAPI的哪些部分按预期工作


    更多信息可以在这里找到:

    我有更多的信息。我知道十六进制后缀是object_id,但我仍然不明白为什么链接不起作用。这和路线钥匙或链接有关吗?!我在这里猜,所以您能提供的任何信息都将不胜感激!谢谢Gareth
    [#<System id="1", name="CIS">]