Rails has_many通过任何? /空?

时间:2013-03-19 10:44:31

标签: ruby-on-rails ruby

我在rails 3 app上发现了一个不寻常的情况。

考虑以下模型:

class Genre < ActiveRecord::Base
  has_many :banner_genres, :dependent => :destroy
  has_many :banners, :through => :banner_genres
  ...

class BannerGenre < ActiveRecord::Base
  attr_accessible :banner_id, :banner, :genre_id, :genre, :position

  belongs_to :genre
  belongs_to :banner
  ...

class Banner < ActiveRecord::Base
  has_many :banner_genres, :dependent => :destroy
  has_many :genres, :through => :banner_genres
  ...

现在,如果我有一个带横幅的流派,我会在rails console中获得以下几行:

1.9.3p362 :005 > g = Genre.find 62
  Genre Load (0.8ms)  SELECT "genres".* FROM "genres" WHERE "genres"."id" = $1 LIMIT 1  [["id", 62]]
 => #<Genre id: 62, ...

1.9.3p362 :006 > g.banner_genres.any?
   (0.5ms)  SELECT COUNT(*) FROM "banner_genres" WHERE "banner_genres"."genre_id" = 62
 => true 

1.9.3p362 :007 > g.banners
  Banner Load (1.0ms)  SELECT "banners".* FROM "banners" INNER JOIN "banner_genres" ON "banners"."id" = "banner_genres"."banner_id" WHERE "banner_genres"."genre_id" = 62 ORDER BY position
 => [#<Banner id: 446, ...

1.9.3p362 :008 > g.banners.any?
 => false 

为什么.any?会返回false?我和另一个人在同一个项目中通过关联获得了它,并返回true

修改:

我在这里传递代码时输了一个错字。它是has_many上的banner_genres。不是genre_banners。

反向关联的工作原理如下:

1.9.3p362 :004 > b = Banner.find 446
  Banner Load (1.1ms)  SELECT "banners".* FROM "banners" WHERE "banners"."id" = $1 ORDER BY position LIMIT 1  [["id", 446]]
 => #<Banner id: 446...

1.9.3p362 :005 > b.genres.any?
 (0.8ms)  SELECT COUNT(*) FROM "genres" INNER JOIN "banner_genres" ON "genres"."id" = "banner_genres"."genre_id" WHERE "banner_genres"."banner_id" = 446
 => true 

修改2

更奇怪的控制台输出:

1.9.3p362 :007 > g.banners.class
 => Array 
1.9.3p362 :008 > g.banners.any?
 => false 
1.9.3p362 :004 > g.banners.any? {|b| b}
 => true 
1.9.3p362 :006 > g.banners.count
 (0.9ms)  SELECT COUNT(*) FROM "banners" INNER JOIN "banner_genres" ON "banners"."id" = "banner_genres"."banner_id" WHERE "banner_genres"."genre_id" = 62
 => 1 
1.9.3p362 :009 > g.banners.to_a.any?
 => true 

编辑3

g.banner_genres
  BannerGenre Load (0.7ms)  SELECT "banner_genres".* FROM "banner_genres" WHERE "banner_genres"."genre_id" = 62
 => [#<BannerGenre id: 4, genre_id: 62, banner_id: 446, position: 1, created_at: "2013-03-15 16:41:10", updated_at: "2013-03-15 16:41:10">]

编辑4 Aleks提问

你可以在g.banners.any上显示查询吗?和g.banners。  这是完全相同的查询,它甚至更奇怪。我也没有覆盖任何?方法无处不在。

1.9.3p362 :037 > g.banners(true)
  Banner Load (1.0ms)  SELECT "banners".* FROM "banners" INNER JOIN "banner_genres" ON "banners"."id" = "banner_genres"."banner_id" WHERE "banner_genres"."genre_id" = 62 ORDER BY position
 => [#<Banner id: 446, ...
1.9.3p362 :038 > g.banners(true).any?
  Banner Load (1.2ms)  SELECT "banners".* FROM "banners" INNER JOIN "banner_genres" ON "banners"."id" = "banner_genres"."banner_id" WHERE "banner_genres"."genre_id" = 62 ORDER BY position
 => false 

1.9.3p362 :039 > g.banners.method(:any?)
 => #<Method: Array(Enumerable)#any?> 

2 个答案:

答案 0 :(得分:1)

这里真正的问题不是关联是好还是坏,而是为什么它为包含元素的数组显示false。 很明显它是返回值,但问题是它返回false

的原因

解释可能在这里:Unable to get Ruby's #any? to return false with list of nil objects

了解any?的实施方式以及它与您的问题的关系,它可能会给您一些提示。

也可以看到此链接:http://apidock.com/ruby/Enumerable/any%3F

修改

你在说什么: 首先,你有一个objects数组。哪个会在any?返回false

但如果你做.to_a,你会做这样的事情:

Time.new.to_a   #=> [39, 54, 8, 9, 4, 2013, 3, 99, true, "CET"]

这就是为什么它将返回true,因为你将拥有非null的对象。

编辑2

请注意:如果数组中的所有对象都是any?false

nil将返回false

答案 1 :(得分:1)

any?应该返回true .... 我对这个问题很好奇,于是我决定模仿它。

g = Genre.find 1
Genre Load (30.1ms)  SELECT "genres".* FROM "genres" WHERE "genres"."id" = $1 LIMIT 1  [["id", 1]]
=> #<Genre id: 1, name: "a", created_at: "2013-03-19 11:44:32", updated_at: "2013-03-19 11:44:32"> 

g.banner_genres.any?
(0.3ms)  SELECT COUNT(*) FROM "banner_genres" WHERE "banner_genres"."genre_id" = 1
=> true 

g.banners
Banner Load (0.5ms)  SELECT "banners".* FROM "banners" INNER JOIN "banner_genres" ON "banners"."id" = "banner_genres"."banner_id" WHERE "banner_genres"."genre_id" = 1
=> [#<Banner id: 1, name: "1", created_at: "2013-03-19 11:43:00", updated_at: "2013-03-19 11:43:00">, #<Banner id: 2, name: "2", created_at: "2013-03-19 11:43:59", updated_at: "2013-03-19 11:43:59">] 

g.banners.any?
=> true 

此外,

g.banners.class
=> Array 
g.banners.any?
=> true 
g.banners.to_a.any?
=> true 

您的代码中还有其他内容......