Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何从模型中收集唯一的对象_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何从模型中收集唯一的对象

Ruby on rails 如何从模型中收集唯一的对象,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个模型“秀”,它有很多“表演”,一个表演有一定的“位置”。 两个或多个演出可能具有相同的位置 我正在寻找一种方法来获取该节目表演使用的所有位置,但只能获取一次(如果有三场表演的位置为X,我只需要获取一次X) 编辑:我现在在这个表单上:一个对象数组[[performance\u id:1,location\u id:1],[performance\u id:2,location\u id:1],[performance\u id:3,location\u id:2]。如何获得包含[1,2](

我有一个模型“秀”,它有很多“表演”,一个表演有一定的“位置”。 两个或多个演出可能具有相同的位置

我正在寻找一种方法来获取该节目表演使用的所有位置,但只能获取一次(如果有三场表演的位置为X,我只需要获取一次X)


编辑:我现在在这个表单上:一个对象数组
[[performance\u id:1,location\u id:1],[performance\u id:2,location\u id:1],[performance\u id:3,location\u id:2]
。如何获得包含[1,2](唯一位置\u id)的数组?

不确定这是否能为您提供帮助

show.performances.collect(&:location).uniq

不确定这是否对你有用

show.performances.collect(&:location).uniq

大概是

Location.joins(:performances).where(:performances => { :show_id => 5 }).group("locations.id")

大概是

Location.joins(:performances).where(:performances => { :show_id => 5 }).group("locations.id")

我在数组中添加了逗号:

[
  [performance_id:1, location_id:1],
  [performance_id:2, location_id:1],
  [performance_id:3, location_id:2]  ].flatten.map {|h| h[:location_id]}.uniq

 => [1, 2] 

我在数组中添加了逗号:

[
  [performance_id:1, location_id:1],
  [performance_id:2, location_id:1],
  [performance_id:3, location_id:2]  ].flatten.map {|h| h[:location_id]}.uniq

 => [1, 2] 

我现在的形式是:一个对象数组[[性能id:1,位置id:1][性能id:2,位置id:1][性能id:3,位置id:2]]。如何获得包含[1,2](唯一位置id)的数组?我现在的形式是:对象数组[[performance\u id:1,location\u id:1][performance\u id:2,location\u id:1][performance\u id:3,location\u id:2]]。如何获得包含[1,2](唯一位置id)的数组?我现在的形式是:对象数组[[performance\u id:1,location\u id:1][performance\u id:2,location\u id:1][performance\u id:3,location\u id:2]]。如何获得包含[1,2](唯一位置id)的数组我现在的形式是:对象数组[[performance\u id:1,location\u id:1][performance\u id:2,location\u id:1][performance\u id:3,location\u id:2]]。如何在Ruby 1.9
uniq
中得到一个包含[1,2](唯一位置id)的数组可以取一个块,所以这可能是
…flatten.uniq{h | h[:location|id]}
,而没有
映射
。在Ruby 1.9
uniq
中可以取一个块,所以这可能就是
…flatte.uniq{h{:location|id]}/code>,没有
映射