Ruby on rails 在rails中添加条件包括
直接讨论我的问题Ruby on rails 在rails中添加条件包括,ruby-on-rails,json,Ruby On Rails,Json,直接讨论我的问题 render :json => projects.to_json(:only => ['name', 'id'],:include => {:client => {:only => ['name']}, :deliverables => {:only => ['name', 'id'], :include => {:tasks => {:only => ['name','id']} } } }) 这就是我的控制器响应
render :json => projects.to_json(:only => ['name', 'id'],:include => {:client => {:only => ['name']}, :deliverables => {:only => ['name', 'id'], :include => {:tasks => {:only => ['name','id']} } } })
这就是我的控制器响应json请求的方式。现在我的问题是,它列出了给定项目中的所有可交付成果和任务,但这里我想用满足特定条件的任务和可交付成果来回应,比如在特定月份创建的所有任务和可交付成果
谢谢,这是预付款。你能给我一个过程吗?(见文件末尾) 我猜代码可能是这样的: 在项目模型中:
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
这对你有用吗?以下是我在项目模型中所做的 有很多:未完成的交付物, :class_name=>“可交付成果”, :条件=>'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']} } } }) }
无论如何,谢谢你们的回复