【问题标题】:Rails rabl condition for class variable in view视图中类变量的Rails rabl条件
【发布时间】:2013-04-15 04:08:56
【问题描述】:

在控制器中我有这样的代码:

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

【问题讨论】:

    标签: ruby-on-rails activerecord rabl


    【解决方案1】:

    您可以利用 ExchangerList 和 CurrencyValues 之间的关系:

    child :currency_values do
      attribute :value
    end
    

    如果您有条件,可以使用 lambda 包含这些条件: 如果 => lambda { |孩子|孩子。什么? })

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

    您可能还想为一个 ExchangerList 对象定义一个视图(即显示):

    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'
    

    或者,如果您将“货币”方法添加到 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
      }
    ],
    

    【讨论】:

    • hem,但是我的代码怎么样?只需添加一个条件?因为 lambda 在这里不好......
    • 如你所见,我在方法中有迭代器
    • 如何在 ruby​​ 循环中执行此操作?喜欢每个
    • 如果可以将迭代器写成ExchangeList模型上的方法,则可以通过RABL作为属性直接调用该方法。我将在上面添加一个代码示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2012-08-25
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多