2

In controller i have such code:

    @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!

and i have such models:

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

i need to display for each ExchangerList it's CurrencyValue with some conditions, as i provided below... But main trouble is with rabl output:

i have such code:

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

as you can see, here for each @bank_exchangers i create node with it's @currencies... But i need to display node only for this @bank_exchangers iterator, if i would write in controller i would write something like:

@currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id, :exchanger_list_id => param}, :order => :updated_at)

How to set something like this in view?

Becouse now my output is like:

{"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}}]}]}

as you can see, for each bank_exchangers i create node with all currency_values data, but i need to put for each bank_exchangers in node only currency_values for this bank_exchangers parent....

How could i do this?

Sorry if something is not clear... i'm new...

Just how to set for my child @currencies in view some condition?

Valdis Azamaris
  • 1,433
  • 5
  • 22
  • 47

1 Answers1

0

You can take advantage of the relation between ExchangerList and CurrencyValues:

child :currency_values do
  attribute :value
end

If you have conditions, you can include these using a lambda: if => lambda { |child| child.something? })

child(:currency_values, if => lambda { |currency_value| currency_value.something? }) do
  attribute :value
end

You may also want to define a view for just the one ExchangerList object (i.e. 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

Then simply have the collection extend this:

collection @bank_exchangers, :root => "bank_exchangers", :object_root => false
extends 'bank_exchangers/show'

Alternatively, if you add a 'currencies' method to to the ExchangerList model, you can call the method directly as an attribute via RABL. Some test code I wrote:

def test
  test_array = []
  1.upto(3) do |i|
    test_array << {qty: i, px: 2*i}
  end
  return test_array
end

You can then simply call this as an attribute:

object @object
attribute :test

This results in the following JSON, which I believe is similar to the format you are trying to achieve:

test: [
  {
    qty: 1,
    px: 2
  },
  {
    qty: 2,
    px: 4
  },
  {
    qty: 3,
    px: 6
  }
],
mikeryz
  • 527
  • 3
  • 8