ActiveRecord获取所有+关联的详细信息

AEonAX

我正在尝试检索所有任务的列表,其中每个任务都有一个开发人员和审阅者。我可以检索该列表,但其中包含developer_id和reviewer_id。如何检索包含开发人员名称和检索者名称的列表?

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的Tasks json。这个问题是跟进的

AEonAX

经过一番搜索发现,as_json(include: <association>)所以这可行

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

但是还需要其他选择。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章