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
查询有许多
宏,并且只能在对象上运行。您所说的只能在项目上运行是什么意思