Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.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,我有一个记录,当我返回一个关联的关联时,它会返回一个集合,其中也包括初始关联的记录。如何过滤掉原始记录?我知道这听起来很混乱,但下面的代码应该是不言自明的 基本上,我有一个记录d,当这个查询返回最终结果-d.parents.flat\u map&:children.uniq时,我不希望它包含d引用的记录。我如何以一种红宝石般的方式做到这一点?如果有一个Rails或Ruby内置的方法可以做到这一点,那就太完美了,所以我可以将它优雅地链接到我现有的查询中,但我怀疑这可能是真的……尽管我很有希望……如

我有一个记录,当我返回一个关联的关联时,它会返回一个集合,其中也包括初始关联的记录。如何过滤掉原始记录?我知道这听起来很混乱,但下面的代码应该是不言自明的

基本上,我有一个记录d,当这个查询返回最终结果-d.parents.flat\u map&:children.uniq时,我不希望它包含d引用的记录。我如何以一种红宝石般的方式做到这一点?如果有一个Rails或Ruby内置的方法可以做到这一点,那就太完美了,所以我可以将它优雅地链接到我现有的查询中,但我怀疑这可能是真的……尽管我很有希望……如果你能提供的话,那就多加一点

[91] pry(main)> d
=> #<User id: 2, email: "def@test.com", encrypted_password: "$2a$Bne..", reset_password_token: nil, reset_password_sent_at: nil, gender: 0>
[92] pry(main)> d.parents
=> [#<User id: 1, email: "abc@test.com", encrypted_password: "$2a$10$...", reset_password_token: nil, reset_password_sent_at: nil, gender: 0>,
 #<User id: 4, email: "jkl@test.com", encrypted_password: "$2a$...", reset_password_token: nil, reset_password_sent_at: nil, gender: 1>]
[94] pry(main)> d.parents.flat_map(&:children).uniq
=> [#<User id: 2, email: "def@test.com", encrypted_password: "$2a$10$...", reset_password_token: nil, reset_password_sent_at: nil, gender: 0>,
 #<User id: 3, email: "ghi@test.com", encrypted_password: "$2a$10$...", reset_password_token: nil, reset_password_sent_at: nil, gender: 1>,
 #<User id: 5, email: "mno@test.com", encrypted_password: "$2a$10$.mXgmN...", reset_password_token: nil, reset_password_sent_at: nil, gender: 1>]
可以使用从Ruby中的数组中筛选元素

例如:

> a = [1,2,3]
> a.select{|x| not x.even?}.map{|x| x**2}
=> [1, 9]
在您的例子中,select子句只会忽略任何等于d的记录

但我认为你是想得到一个用户的兄弟姐妹。如果已进行模型设置,则应执行以下操作:

class User < ActiveRecord::Base
  has_many :parents # with keys and class_name pointing to the same class
  has_many :children # with keys and class_name pointing to the same class

  has_many :parents_children, through: :parents, source: :children

  def siblings
    self.parents_children.where("users.id != ?", self.id)
  end
end

d.siblings # what you want

我想不出传递用户的方法。id!=self.id条件到has\u many relationship,否则has\u many:兄弟姐妹,选项将是理想的。

您可以重写我的原始平面图,以包括select,还是像joel建议的那样,它只是在末尾?您自己适应应该不难。我认为您想要获取用户的兄弟姐妹。看看我的答案。它提供了一个更快的选择。谢谢,我会删除我的,因为它不需要了,伊姆霍-胡姆扎已经做好了准备。我建议更新你的标题以匹配问题,这样有助于人们在这里搜索;也许有一些关键词,比如父母、孩子、兄弟姐妹、activerecord等等。我真的很喜欢这种方法,而这正是我真正想要的。你认为这比我以前做的ruby查询好吗?i、 性能更好?是的。这样会更有效率。除了Ruby过滤之外,您之前还为每个父级查询了一次数据库。这样,您将只查询数据库一次。我刚刚更新了这个问题,并详细介绍了我的关系——特别是我通过另一个模型——关系来处理父母和孩子之间的关系。鉴于这种结构,我将如何应用你的答案?我怀疑你有很多:父母、孩子。。。行得通……不行?应该行得通。在我的例子中,你仍然有很多:父母和孩子返回用户。是的。多谢!
> a = [1,2,3]
> a.select{|x| not x.even?}.map{|x| x**2}
=> [1, 9]
d.parents.flat_map(&:children).uniq.reject{ |c| c == d }
d.parents.flat_map(&:children).uniq - [d]
class User < ActiveRecord::Base
  has_many :parents # with keys and class_name pointing to the same class
  has_many :children # with keys and class_name pointing to the same class

  has_many :parents_children, through: :parents, source: :children

  def siblings
    self.parents_children.where("users.id != ?", self.id)
  end
end

d.siblings # what you want