2015-02-07 150 views
0

我試圖檢索所有任務的列表,其中每個任務都有一個開發人員和審閱者。我能夠檢索列表,但它包含developer_id和reviewer_id。如何檢索包含開發者名稱和檢索者名稱的列表?ActiveRecord獲取所有+相關聯的詳細信息

class Person < ActiveRecord::Base 
end 

class Unread_Object < ActiveRecord::Base 
    belongs_to :person 
end 

class Developer < Person 
    has_many :tasks 
end 
class Reviewer < Person 
    has_many :tasks 
    has_many :unread_objects 
end 

class Task < ActiveRecord::Base 
    belongs_to :developer 
    belongs_to :reviewer 
    has_many :documents 
    after_save :add_task_to_unread_objects 

    protected 
    def add_task_to_unread_objects 
     Person.find_each do |person| 
     Unread_Object.create(
           :person_id => person.id, 
           :internal_object_id => self.internal_object_id, 
           :unread_cause => 'Create') 
     end 
    end 
end 

我試過的東西。

get '/taskslist' do 
    #Task.includes([:developer, :reviewer]).all.to_json 
    #Task.joins(:developer,:reviewer).select("tasks.*, people.*").to_json #works somewhat but only shows one name 
    #Task.includes(:reviewer.name,:developer.name).all.to_json #"undefined method `name' for :reviewer:Symbol" 
    #Task.find(:all, :include => {:people => :name}).to_json #Couldn't find all Tasks with 'id': (all, {:include=>{:people=>:name}}) 
end 

我希望爲開發人員,審閱者和其他對象獲取任務json與嵌套的json。 這個問題是跟進this。某些搜索找到as_json(include: <association>)

回答

0

後,所以此工程

Task.includes(:developer,:reviewer).all.as_json(include: [:developer,:reviewer]).to_json 

但其他的替代品需要進一步觀察。

相關問題