2012-08-31 2 views
0

관련 레코드가있는 모델에서 모든 레코드를 가져온 다음 .to_json으로 json을 생성하려고합니다. 나는 레일을 사용하고있다 2.3. 여기 연관성이있는 Model.all을 가져 와서 json으로 돌아 가기

모델의 일부 조각 :

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 

그러나 결과는 exacly 같은입니다 단순히 할 것 같은 Currency.all.to_json. ActiveRecord를 사용하여 나의 목표를 달성하는 것이 불가능하다면 어떤 종류의 SQL을 사용해야하는지 안내해주십시오.

답변

2

이 작업을 수행 :

Currency.all(:include => [:exchange_from, :exchange_to]).to_json(:include => [:exchange_from, :exchange_to]) 

Documentation here.