Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 Rails-如何自动使用uniq连接方法?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails Rails-如何自动使用uniq连接方法?

Ruby on rails Rails-如何自动使用uniq连接方法?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,这个问题是基于这样的原因: 假设我通过.joins()方法获得非uniq数组: City.joins(:locations) # => [#<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", upd

这个问题是基于这样的原因:

假设我通过
.joins()
方法获得非uniq数组:

City.joins(:locations)
# => [#<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">]
City.join(:位置)
# => [#, #, #, #]
我可以使用uniq制作记录

City.joins(:locations).group('cities.id') # or simpler
City.joins(:locations).uniq
# => [#<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">]
City.joins(:locations).group('cities.id')#或更简单
City.joins(:locations).uniq
# => [#]

如何使
.joins()
方法在默认情况下返回uniq记录?

您可以尝试覆盖所需模型的
.joins
方法,但我建议只编写一个范围,例如

scope :unique_locations, -> { joins(:locations).uniq }
然后只需拨打
City.unique\u locations
。这样更干净,可读性更强


通常,只有当您确定不再需要“旧方法”时,才应该执行覆盖方法,而且这样做是有意义的。另外,当你说
City.joins(:locations)
时,读者期望默认行为,而返回其他内容将导致混乱和混乱。

你可以尝试覆盖你需要的模型的
.joins
方法,但我建议只编写一个范围,例如

scope :unique_locations, -> { joins(:locations).uniq }
然后只需拨打
City.unique\u locations
。这样更干净,可读性更强


通常,只有当您确定不再需要“旧方法”时,才应该执行覆盖方法,而且这样做是有意义的。另外,当你说
City.joins(:locations)
时,读者期望默认行为,而返回其他内容将导致混乱和混乱。

你可以尝试覆盖你需要的模型的
.joins
方法,但我建议只编写一个范围,例如

scope :unique_locations, -> { joins(:locations).uniq }
然后只需拨打
City.unique\u locations
。这样更干净,可读性更强


通常,只有当您确定不再需要“旧方法”时,才应该执行覆盖方法,而且这样做是有意义的。另外,当你说
City.joins(:locations)
时,读者期望默认行为,而返回其他内容将导致混乱和混乱。

你可以尝试覆盖你需要的模型的
.joins
方法,但我建议只编写一个范围,例如

scope :unique_locations, -> { joins(:locations).uniq }
然后只需拨打
City.unique\u locations
。这样更干净,可读性更强


通常,只有当您确定不再需要“旧方法”时,才应该执行覆盖方法,而且这样做是有意义的。另外,当您说
City.joins(:locations)
时,读者期望默认行为,而返回其他内容将导致混乱和混乱。

您可以定义
有许多
宏,并将
stubby
lambda作为参数:

has_many :locations, -> { joins(:locations).uniq }
您还可以定义自己的AR关系方法,它仍然使用一个简单的

has_many :locations do
  def only_uniq     
    joins(:locations).uniq
  end
end
现在使用它:

c = City.find(123)
c.locations.only_uniq

它与
has\u many
中的
scope
lambda
的作用相同
您可以定义
has\u many
宏,并将
stubby
lambda作为参数:

has_many :locations, -> { joins(:locations).uniq }
您还可以定义自己的AR关系方法,它仍然使用一个简单的

has_many :locations do
  def only_uniq     
    joins(:locations).uniq
  end
end
现在使用它:

c = City.find(123)
c.locations.only_uniq

它与
has\u many
中的
scope
lambda
的作用相同
您可以定义
has\u many
宏,并将
stubby
lambda作为参数:

has_many :locations, -> { joins(:locations).uniq }
您还可以定义自己的AR关系方法,它仍然使用一个简单的

has_many :locations do
  def only_uniq     
    joins(:locations).uniq
  end
end
现在使用它:

c = City.find(123)
c.locations.only_uniq

它与
has\u many
中的
scope
lambda
的作用相同
您可以定义
has\u many
宏,并将
stubby
lambda作为参数:

has_many :locations, -> { joins(:locations).uniq }
您还可以定义自己的AR关系方法,它仍然使用一个简单的

has_many :locations do
  def only_uniq     
    joins(:locations).uniq
  end
end
现在使用它:

c = City.find(123)
c.locations.only_uniq


它的作用与
中的
范围
lambda
的作用相同。。好主意。也许,还有别的办法吗?嗯。。好主意。也许,还有别的办法吗?嗯。。好主意。也许,还有别的办法吗?嗯。。好主意。也许,还有另一种方法可以做到这一点呢?
有很多:位置,->{joins(:locations)。uniq}
?它能改变其他地方的请求行为吗?似乎正是我想要的…是的。它改变了
sql
查询有很多个
宏,并且只能在一个对象上运行。你所说的
只能在项目上运行是什么意思?
项目
一个
对象
有多少个:位置,->{连接(:位置)。uniq}
?它能改变其他地方的请求行为吗?似乎正是我想要的…是的。它改变了
sql
查询有很多个
宏,并且只能在一个对象上运行。你所说的
只能在项目上运行是什么意思?
项目
一个
对象
有多少个:位置,->{连接(:位置)。uniq}
?它能改变其他地方的请求行为吗?似乎正是我想要的…是的。它改变了
sql
查询有很多个
宏,并且只能在一个对象上运行。你所说的
只能在项目上运行是什么意思?
项目
一个
对象
有多少个:位置,->{连接(:位置)。uniq}
?它能改变其他地方的请求行为吗?似乎正是我想要的…是的。它改变了
sql
查询有许多
宏,并且只能在对象上运行。您所说的
只能在项目上运行是什么意思