直接解决我的问题。
render :json => projects.to_json(:only => ['name', 'id'],:include => {:client => {:only => ['name']}, :deliverables => {:only => ['name', 'id'], :include => {:tasks => {:only => ['name','id']} } } })
这是我的控制器响应json请求的方式。现在我的问题是它列出了所有的可交付成果&在给定项目中的任务但在这里我想用任务和&满足特定条件的可交付成果,例如在特定月份创建的所有条件。
谢谢你的到来。
答案 0 :(得分:2)
这是我在项目模型中所做的事情
has_many:uncompleted_deliverables, :class_name => “交付”, :conditions => 'completed = false'
这同样适用于可交付模型
has_many :uncompleted_tsks, :class_name => 'Task', :conditions => 'completed = false'
然后以下列方式回复了json,
format.json { render :json => projects.to_json(:only => ['name', 'id'],
:include => {:client => {:only => ['name']}, :uncompleted_deliverables => {:only => ['name', 'id'], :include => {:uncompleted_tsks => {:only => ['name','id']} } } }) }
无论如何,谢谢你的回复......
答案 1 :(得分:0)
我猜代码看起来像这样:
项目模型中的:
def to_json(options={})
tasks_json = Proc.new { |options|
options[:builder].tasks do
selected_tasks = tasks.select {|t| t.created_at > 30.days.ago } # or whatever your condition is
selected_tasks.each do |t|
options[:builder].task do
options[:builder].tag!('id', t.id)
options[:builder].tag!('name', t.name)
end
end
end }
options.merge!(:procs => [tasks_json])
end
这会对你有用吗?