对返回对象的行为感到困惑
Confused about the behavior of returned object
发射 Model.find_by_sql 之后;
data = Setup::Type.find_by_sql ["SELECT value FROM table WHERE type_cd = 'print_format' AND subtype_cd = 'schedule_print_format'"]
对象返回为;
#<Setup::Type:0x60c42f0>
#<Setup::Type:0x60c4140>
#<Setup::Type:0x60c3f90>
在数据上使用 'inspect' 函数时,它会重新运行
[#<Setup::Type value: "SalesReceipt_Bhindi_sch.rpt">, #<Setup::Type value: "SpecialOrder_Bhindi_sch.rpt">, #<Setup::Type value: "ReturnReceipt_Bhindi_sch.rpt">, #<Setup::Type value: "Takepayment_Bhindi_sch.rpt">]
关于使用data.class它returns数组。
但是,据我推测,这在用作 'hash' 时效果很好,
data.each do |name|
xml = Hpricot::XML(%{
<params>
<from_trans_date>#{date_for_transaction}</from_trans_date>
<to_trans_date>#{date_for_transaction}</to_trans_date>
<print_format>#{name.value}</print_format>
<company_id>#{company_id}</company_id>
</params>
})
我的问题是,根据 'name.value'.
的实现,为什么 'data' 表现得像哈希
谢谢
find_by_sql
正在返回 Setup::Type
个对象的数组。当您遍历数组时,您将获得一个 Setup::Type
的实例,您正在使用 name
变量访问它。因此 name.value
正在工作。
如果把name
改成type_obj
就更清楚了,
data.each do |type_obj|
xml = Hpricot::XML(%{
<params>
<from_trans_date>#{date_for_transaction}</from_trans_date>
<to_trans_date>#{date_for_transaction}</to_trans_date>
<print_format>#{type_obj.value}</print_format>
<company_id>#{company_id}</company_id>
</params>
})
这来自 find_by_sql 的文档。可能有帮助。
Executes a custom SQL query against your database and returns all the
results. The results will be returned as an array with columns
requested encapsulated as attributes of the model you call this method
from. If you call Product.find_by_sql then the results will be
returned in a Product object with the attributes you specified in the
SQL query.
发射 Model.find_by_sql 之后;
data = Setup::Type.find_by_sql ["SELECT value FROM table WHERE type_cd = 'print_format' AND subtype_cd = 'schedule_print_format'"]
对象返回为;
#<Setup::Type:0x60c42f0>
#<Setup::Type:0x60c4140>
#<Setup::Type:0x60c3f90>
在数据上使用 'inspect' 函数时,它会重新运行
[#<Setup::Type value: "SalesReceipt_Bhindi_sch.rpt">, #<Setup::Type value: "SpecialOrder_Bhindi_sch.rpt">, #<Setup::Type value: "ReturnReceipt_Bhindi_sch.rpt">, #<Setup::Type value: "Takepayment_Bhindi_sch.rpt">]
关于使用data.class它returns数组。
但是,据我推测,这在用作 'hash' 时效果很好,
data.each do |name|
xml = Hpricot::XML(%{
<params>
<from_trans_date>#{date_for_transaction}</from_trans_date>
<to_trans_date>#{date_for_transaction}</to_trans_date>
<print_format>#{name.value}</print_format>
<company_id>#{company_id}</company_id>
</params>
})
我的问题是,根据 'name.value'.
的实现,为什么 'data' 表现得像哈希谢谢
find_by_sql
正在返回 Setup::Type
个对象的数组。当您遍历数组时,您将获得一个 Setup::Type
的实例,您正在使用 name
变量访问它。因此 name.value
正在工作。
如果把name
改成type_obj
就更清楚了,
data.each do |type_obj|
xml = Hpricot::XML(%{
<params>
<from_trans_date>#{date_for_transaction}</from_trans_date>
<to_trans_date>#{date_for_transaction}</to_trans_date>
<print_format>#{type_obj.value}</print_format>
<company_id>#{company_id}</company_id>
</params>
})
这来自 find_by_sql 的文档。可能有帮助。
Executes a custom SQL query against your database and returns all the results. The results will be returned as an array with columns requested encapsulated as attributes of the model you call this method from. If you call Product.find_by_sql then the results will be returned in a Product object with the attributes you specified in the SQL query.