在控制器中我有这样的代码:
@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?