在控制器中我有这样的代码:
@bank_exchangers = ExchangerList.find(:all, :conditions => {:bank_id => params[:id]})
@currency_list = CurrencyList.all
@currencies = []
@currency_list.each do |c|
@currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id}, :order => :updated_at)
end
@currencies.flatten!
我有这样的模特:
class CurrencyList < ActiveRecord::Base
attr_accessible :code, :country, :name
has_many :currency_values
end
class CurrencyValue < ActiveRecord::Base
attr_accessible :currency_list_id, :direction_of_exchange_id, :value
belongs_to :currency_list
belongs_to :exchanger_list
end
class ExchangerList < ActiveRecord::Base
attr_accessible :address, :bank_id, :exchanger_type_id, :latitude, :location_id, :longitude, :name
has_many :currency_values
end
我需要为每个ExchangerList显示它的CurrencyValue有一些条件,正如我在下面提供的那样......但主要的麻烦是rabl输出:
我有这样的代码:
collection @bank_exchangers, :root => "bank_exchangers", :object_root => false
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
child @currencies do
attribute :value
end
正如你所看到的,这里为每个@bank_exchangers我用@currencies创建节点...但我只需要为这个@bank_exchangers迭代器显示节点,如果我在控制器中写,我会写如下:
@currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id, :exchanger_list_id => param}, :order => :updated_at)
如何在视图中设置这样的内容?
现在我的输出就像:
{"bank_exchangers":[{"id":3,"name":"Банк *** ЦБУ №1","address":"г. Минск, ул. Московская, 13","location_id":8,"latitude":null,"longitude":null,"exchanger_type_id":1,"location_name":"Минск","exchanger_type_name":"normal","currency_values":[{"currency_value":{"value":8620.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":11500.0}},{"currency_value":{"value":11100.0}}]},{"id":4,"name":"Банк ***","address":"г. Минск, Мясникова, 32","location_id":8,"latitude":null,"longitude":null,"exchanger_type_id":1,"location_name":"Минск","exchanger_type_name":"normal","currency_values":[{"currency_value":{"value":8620.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":8620.0}},{"currency_value":{"value":8700.0}},{"currency_value":{"value":11500.0}},{"currency_value":{"value":11100.0}}]}]}
正如您所看到的,对于每个bank_exchangers,我使用所有currency_values数据创建节点,但我需要为节点中的每个bank_exchangers仅为该bank_exchangers父节点设置currency_values ....
我怎么能这样做?
很抱歉,如果事情不清楚......我是新人......
在某种情况下,如何为我的孩子设置@currencies?
答案 0 :(得分:0)
您可以利用ExchangerList和CurrencyValues之间的关系:
child :currency_values do
attribute :value
end
如果您有条件,可以使用lambda包含这些条件: if =&gt; lambda {| child | child.something? })
child(:currency_values, if => lambda { |currency_value| currency_value.something? }) do
attribute :value
end
您可能还想为一个ExchangerList对象(即show)定义一个视图:
object @bank_exchanger, :object_root => false
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
child(:currency_values, if => lambda { |currency_value| currency_value.something? }) do
attribute :value
end
然后简单地让集合扩展这个:
collection @bank_exchangers, :root => "bank_exchangers", :object_root => false
extends 'bank_exchangers/show'
或者,如果将“currency”方法添加到ExchangerList模型,则可以通过RABL直接将该方法作为属性调用。我写的一些测试代码:
def test
test_array = []
1.upto(3) do |i|
test_array << {qty: i, px: 2*i}
end
return test_array
end
然后您可以将其称为属性:
object @object
attribute :test
这导致以下JSON,我认为它与您尝试实现的格式相似:
test: [
{
qty: 1,
px: 2
},
{
qty: 2,
px: 4
},
{
qty: 3,
px: 6
}
],