简体   繁体   English

Databasedotcom在Ruby中实现自定义对象

[英]Databasedotcom materialize a custom object in Ruby

I would like to return the results of a SOQL query as JSON, but the data seems to be returned as a string. 我想以JSON形式返回SOQL查询的结果,但是数据似乎以字符串形式返回。

client = SFDC_Adapter.login
data = client.query("SELECT  MarkupAmount__c, 
                        MarkupPercent__c,
                        Product_Type_Id__c,
                        Product_Type__c 
                        FROM Product_Type__c 
                        WHERE Product_Type_Id__c = #{product_type_id}")
p data

=> [#<Product_Type__c:0x00000001c356f8 @Id=nil, @OwnerId=nil, @IsDeleted=nil, @Name=nil, @CreatedDate=nil, @CreatedById=nil, @LastModifiedDate=nil, @LastModifiedById=nil, @SystemModstamp=nil, @MarkupPercent__c=5.0, @Subscription__c=nil, @Product_Type__c="Research Trip", @MarkupAmount__c=nil, @Product_Type_Id__c=36.0>]

    puts data
=> #<Product_Type__c:0x00000001c356f8>

    puts data.to_json
=> ["#<Product_Type__c:0x00000001c356f8>"]

How do I materialize these results into a JSON object for use in a Restful service? 如何将这些结果具体化为JSON对象以在Restful服务中使用?

I don't know that gem, but from looking at your output, and glancing at your results, it looks like you got a Product_Type object back. 我不知道该gem,但是通过查看输出并浏览结果,您似乎又获得了Product_Type对象。

When you use p or puts , inspect is being used, which is turning the instance into something viewable in a web-page, by using an HTML encoding on it. 当您使用pputs ,将使用inspect ,这是通过在实例上使用HTML编码将实例变成在网页中可见的内容。 That's why you see < 这就是为什么您看到< and > > in the output. 在输出中。

Instead, you need to access the values in the object. 相反,您需要访问对象中的值。 According to the docs , you can use standard getters or using a hash[key] form to do that: 根据文档 ,您可以使用标准的getter或使用hash[key]形式来做到这一点:

contact = Contact.find("contact_id")                #=> #
contact = Contact.find_by_Name("John Smith")        #=> dynamic finders!
contacts = Contact.all                              #=> a Databasedotcom::Collection of Contact instances
contacts = Contact.find_all_by_Company("IBM")       #=> a Databasedotcom::Collection of matching Contacts
contact.Name                                        #=> the contact's Name attribute
contact["Name"]                                     #=> same thing
contact.Name = "new name"                           #=> change the contact's Name attribute, in memory
contact["Name"] = "new name"                        #=> same thing
contact.save                                        #=> save the changes to the database
contact.update_attributes "Name" => "newer name",
  "Phone" => "4156543210"                           #=> change several attributes at once and save them
contact.delete                                      #=> delete the contact from the database

Try data['Product_Type_Id'] and you should get 36.0 . 试试data['Product_Type_Id'] ,您应该得到36.0 An alternate way of doing the same thing is data.Product_Type_Id . 做同一件事的另一种方法是data.Product_Type_Id

Once you have your accessors figured out you can generate JSON using a simple hash or array of hashes. 确定访问器后,您可以使用简单的哈希或哈希数组生成JSON。 This would generate a hash: 这将生成一个哈希:

require 'json'

hash = {
    'Id'               => data.Id,
    'OwnerId'          => data.OwnerId,
    'IsDeleted'        => data.IsDeleted,
    'Name'             => data.Name,
    'CreatedDate'      => data.CreatedDate,
    'CreatedById'      => data.CreatedById,
    'LastModifiedDate' => data.LastModifiedDate,
    'LastModifiedById' => data.LastModifiedById,
    'SystemModstamp'   => data.SystemModstamp,
    'MarkupPercent'    => data.MarkupPercent,
    'Subscription'     => data.Subscription,
    'Product_Type'     => data.Product_Type,
    'MarkupAmount'     => data.MarkupAmount,
    'Product_Type_Id'  => data.Product_Type_Id,
  }

puts hash.to_json

I didn't see a to_h or to_hash method which would be a shortcut. 我没有看到to_hto_hash方法是快捷方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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