传入数组的活动记录选择

时间:2012-12-13 06:15:47

标签: ruby-on-rails activerecord select

如何显示status.name等于数组中项目的所有故障单?

带有select的单个项目似乎就像这样工作

@project_tickets = @project.tickets.select { |m| m.status.name == "new"] }

但没有项目数组

params[:array_here] = ["new","implemented"]

@project = Project.find_by_id(params[:project_id])
@project_tickets = @project.tickets.select { |m| m.status.name == [params[:array_here]] }

4 个答案:

答案 0 :(得分:2)

你要求的方式是这样的:

@project_tickets = @project.tickets.select { |m| params[:array_here].include? m.status.name }

但是你应该把它委托给数据库。

@project_tickets = Ticket.includes(:status).where(:project_id => @project.id, :status => { :name => params[:array_here] })

答案 1 :(得分:0)

where不起作用吗?

@project_tickets = @project.tickets.where("name = ?", params[:array_here])

答案 2 :(得分:0)

 @new_tickets=@project.tickets.select { |m| m.status.name == params[:array_here][0] }

 @implemented_tickets=@project.tickets.select { |m| m.status.name == params[:array_here][1] }

答案 3 :(得分:0)

params[:array_here] = ["new","implemented"]

@project = Project.find_by_id(params[:project_id])
@project_tickets = @project.tickets.select { |m| params[:array_here].include?(m.status.name) }