我正在尝试从模型中获取所有记录以及相关记录,然后使用.to_json
生成json。我正在使用Rails 2.3 。
以下是该模型的一部分:
class Currency
has_many :exchange_from, :class_name => "CurrencyExchange", :foreign_key => "currency_from_id", :dependent => :destroy
has_many :exchange_to, :class_name => "CurrencyExchange", :foreign_key => "currency_to_id", :dependent => :destroy
这就是我认为可行的代码:
Currency.all(:include => [:exchange_from, :exchange_to]).to_json
但结果与我简单地Currency.all.to_json
显然是相同的。如果使用ActiveRecord无法实现我的目标,请指导我应该使用哪种SQL。
答案 0 :(得分:2)
这样做:
Currency.all(:include => [:exchange_from, :exchange_to]).to_json(:include => [:exchange_from, :exchange_to])